SoFunction
Updated on 2025-03-10

Apache's Rewrite experience

Apache's Rewrite experience

Recently, the company is going to transfer the server. There are dozens of sites running on the server. I hope that the service will not be affected during the transfer period, so I used Apache's mod_rewrite to do some processing.
The general environment is like this: the server has site[1-50]. Domain name and site[1-50]. Domain name. Before starting to transfer the server, point site[1-50]. to the new server IP. At the same time, run Apache on the new IP and access Rewrite back. After a day, it will be parsed to the new IP. At the same time, transfer the database and WEB services to the new IP, and transfer Apache Rewrite on the original server to the access to some users cache DNS, resulting in still accessing the original server.

The writing of Rewrite is roughly as follows:


Copy the codeThe code is as follows:
#Load mod_rewrite
LoadModule rewrite_module modules/mod_rewrite.so 

#Open mod_rewrite
RewriteEngine On 
#Enable Log (for debugging)
RewriteLog "logs/" 
RewriteLogLevel 9 
#Only use the domain names starting with siteX, demo, and support as Rewrite
RewriteCond %{SERVER_NAME} ^(site|demo|support) [NC] 
#Put the host name before the URI
RewriteRule ^(.+) %{SERVER_NAME}$1 [C] 
#Go all /* of this server to /*
RewriteRule  ^([a-z0-9]+)\.([0-9a-z\.]+)/(.*)  http://$1\.TB\.com/$3 [NC,R,L] 


If Apache has configured a virtual host, this configuration should be written to the default virtual host configuration.