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 used mod_rewrite and with a lot of trial and error and some updates i finally got this working 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
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_METHOD} ^TRACE
RewriteRule .* - [F]
##### WordPress Rewrites
# redirects http://<SERVER_A>/ to the blog
RewriteRule ^/$ /blog/
# needed for xmlrpc to work
RewriteRule ^/blog/xmlrpc.php(.*) http://<SERVER_B>/<PATH_TO_WORDPRESS>/xmlrpc.php$1 [L,P]
# needed for language chooser to work on pages (not posts)
RewriteRule ^/blog/index.php/(.*)$ /blog/$1
# rewrite everything to index.php, except pages ending with .php or directories inside wp-*
RewriteCond %{REQUEST_URI} !^/blog/.*\.php$
RewriteCond %{REQUEST_URI} !^/blog/wp-.*
RewriteRule ^/blog(.*) http://<SERVER_B>/<PATH_TO_WORDPRESS>/index.php$1 [L,P]
# special rewrites for special cases
# contet directory rewrite
RewriteRule ^/blog/wp-(.*)$ http://<SERVER_B>/<PATH_TO_WORDPRESS>/wp-$1 [L,P]
# trailin slash problem
RewriteRule ^/blog$ http://<SERVER_B>/wordpress/ [L,P]
</IfModule>
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 😉
UPDATE for WordPress > 2.3:
wordpress 2.3 introduced some rewrite magic implemented in wp-includes/canonical.php. Unfortunately this doesn’t work with my rewrite rules, therefore i had to disable this feature:
edit <yourwordpressdirectory>/wp-includes.php and replace
add_action('template_redirect', 'redirect_canonical');
at the end of the file with
//add_action('template_redirect', 'redirect_canonical');
note: this has to be done each time you update your wordpress version!!!