I launched one of my client's sites last night, and wanted to make the old pages automatically redirect to the new ones.
I could have written a manual series of Apache Rewrite Rules to this, but the majority of the page names stayed the same, so I was hoping I could get a bit fancier than that. I worked up the following rule:
RewriteCond %{REQUEST_URI} ^/(.*) RewriteCond newdir/%1 -f RewriteRule ^(.*) http://www.clientsite.com/newdir/$1 [R=301,L]
Which does the follow:
- Grabs everything after the slash in the request URI and stores it in %1 (/foo.php stores foo.php in %1)
- Checks for the existing of the file in newdir (does newdir/foo.php exist?)
- If so, then redirect to that URL
Alas, this wasn't working. I figured out that the -f check was failing. The fix? To not assume the relative path newdir/foo.php would be found. Instead, use the %{DOCUMENT_ROOT} variable to build up and compare an absolute path.
RewriteCond %{REQUEST_URI} ^/(.*) RewriteCond %{DOCUMENT_ROOT}/newdir/%1 -f RewriteRule ^(.*) http://www.customersite.com/newdir/$1 [R=301,L]
Man I love Apache Rewrite Rules - they can be cryptic, but well worth getting to to know and master.
No comments:
Post a Comment