Howto hide a worpress instance behind a different apache server with the mod_rewrite.
I had the need to move my blog running wordpress 2.0.x to a different server, but keeping the domain name on the “old” server. I tried to do this with mod_rewrite and succeeded only partially :-(
after some hours of hacking around with mod_rewrite and wordpress itself i got i working and want to share my solution:
note: let’s call the origin server SERVER A and the “new” server running wordpress and mySQL SERVER B. SERVER A has only Apache 2 with mod_rewrite running, SERVER B has Apache, mySQL and PHP running.
Step 1: basic rewrite rules on SERVER A
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/blog/.*\.php$
RewriteCond %{REQUEST_URI} !^/blog/wp-.*
RewriteRule ^/blog(.*) http://<SERVER B>/<path to wordpress>/index.php$1 [L,P]
RewriteRule ^/blog$ http://<SERVER B>/<path to wordpress>/ [L,P]
RewriteRule ^/blog(.*)$ http://<SERVER B>/<path to wordpress>$1 [L,P]
note: my blog runs on http://<SERVER A>/blog/
Step 2: configure WordPress
login to wordpress (best done before moving wordpress to SERVER B) and go to the options -> general screen in wordpress admin and set these two values:
WordPress address (URI): http://<SERVER B>/<path to wordpress>
Blog address (URI): http://<SERVER A>/blog
Step 3: hack WordPress
now comes the part that took me hours to figure out. With all the above, wordpress almost worked, but index.php always threw a 404 not found error :-(
The problem was, that Wordpress uses $_SERVER['REQUEST_URI'] to build its links and stuff, but for my setup REQUEST_URI is <path to wordpress on SERVER B>… but this isn’t right, it should be simply /blog/…
this inserted into my config.php file does the trick:
$_SERVER['REQUEST_URI'] = str_replace(’/<path to wordpress on SERVER B>/’, ‘/blog/’, $_SERVER['REQUEST_URI']);
Step 4: there’s no step 4 ;-)
Technorati Tags: mod_rewrite, networking, sysadmin, webdesign