TheNextman
TheNextman

Reputation: 12566

Rewriting Wordpress post URL for new domain

I have a Wordpress site running on EC2. It is hosted in a directory, like this:

www.olddomain.ca/blog

We are migrating to a new URL, so I duplicated the site in a new website:

www.newdomain.com

I would like to redirect users accessing posts on the old domain, to the equivalent post on the new domain. I believe that mod-rewrite is the right tool to do this. However, my user has done something with the Wordpress configuration on the new site that makes the URLs different.

An example:

http://www.olddomain.ca/blog/?p=296 becomes http://www.newdomain.com/index.php/p296

I'm having trouble making this work - I've never used mod-rewrite and this seems to be a complex scenario. Any help?

Upvotes: 1

Views: 81

Answers (1)

anubhava
anubhava

Reputation: 785216

Place this code in your DOCUMENT_ROOT/.htaccess file of old domain:

RewriteEngine On

# URIs without any p=... query string
RewriteCond %{HTTP_HOST} olddomain\.ca$ [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^blog/(.*)$ http://www.newdomain.com/$1 [NC,NE,R=301,L]

# URIs with p=... query string
RewriteCond %{HTTP_HOST} olddomain\.ca$ [NC]
RewriteCond %{QUERY_STRING} (?:^|&)p=([^&]+) [NC]
RewriteRule ^blog/?$ http://www.newdomain.com/index.php/p%1? [NC,NE,R=301,L]

Upvotes: 1

Related Questions