Mobile app version of vmapp.org
Login or Join
Welton855

: Moving a WordPress site to a new domain - need help with an Apache htaccess URL rewrite rule I'm helping to move a WordPress site from one domain to another, which includes a change in permalink

@Welton855

Posted in: #Apache #Htaccess #Wordpress

I'm helping to move a WordPress site from one domain to another, which includes a change in permalink structure. I know I can use a .htaccess entry, but I'm not that well-versed (yet) in regular expressions.

I need to change this:
www.oldwebsite.com/2013/09/title-of-post.html

to this:
www.newwebsite.com/title-of-post

dropping the year, month, and .html from every URL.

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Welton855

1 Comments

Sorted by latest first Latest Oldest Best

 

@Lee4591628

I’ve done this with Apache mod_rewrite and it works great. Just add these three lines to either the .htaccess or Apache2 configuration on the old server. This should work well/cover all .html files as well as other variations:

RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} ^(GET|HEAD) /([^ ]+).html
RewriteRule ^([0-9]{4})/([0-9]{2})/([^ ]+).html?$ www.newwebsite.com/ [L,R=301]
RewriteRule ^([0-9]{4})/([^ ]+).html?$ www.newwebsite.com/ [L,R=301]
RewriteRule ^([0-9]{4})/([0-9]{2})/?$ www.newwebsite.com/ [NC,L,R=301]
RewriteRule ^([0-9]{4})/?$ www.newwebsite.com/ [NC,L,R=301]
RewriteRule ^(.*)$ www.newwebsite.com/ [NC,L,R=301]


So the first two RewriteRule entries handle requests to the following URLs:
www.oldwebsite.com/2013/04/test.html www.oldwebsite.com/2013/test.html


And now send them to:
www.newwebsite.com/test

The next two RewriteRule entries handle requests to these directory level root URLs:
www.oldwebsite.com/2013/04/ http://www.oldwebsite.com/2013/


To this the base URL of your site:
www.newwebsite.com/

And the last RewriteRule is set to grab any other URLs that don’t fit the above criteria and send them to the main URL while passing the full param.

I would suggest that you test these out using curl -I like so:

curl -I www.oldwebsite.com/2013/04/test.html

That will show you the Apache header output & tell you exactly where pages are being redirected without having to deal with a full web browser headaches when debugging stuff like this.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme