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?$ http://www.newwebsite.com/$3 [L,R=301]
RewriteRule ^([0-9]{4})/([^\ ]+)\.html?$ http://www.newwebsite.com/$2 [L,R=301]
RewriteRule ^([0-9]{4})/([0-9]{2})/?$ http://www.newwebsite.com/ [NC,L,R=301]
RewriteRule ^([0-9]{4})/?$ http://www.newwebsite.com/ [NC,L,R=301]
RewriteRule ^(.*)$ http://www.newwebsite.com/$1 [NC,L,R=301]
So the first two RewriteRule
entries handle requests to the following URLs:
http://www.oldwebsite.com/2013/04/test.html
http://www.oldwebsite.com/2013/test.html
And now send them to:
http://www.newwebsite.com/test
The next two RewriteRule
entries handle requests to these directory level root URLs:
http://www.oldwebsite.com/2013/04/
http://www.oldwebsite.com/2013/
To this the base URL of your site:
http://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 http://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.