Sunday, April 23, 2006

Mod_rewite Redirects All Site Pages To One

I had an interesting challenge the other day. One of my non-profit clients had to close their site, wanted to tell the visitors why they were closing and suggest that visitors support another non-profit site. This was due to an internal situation and not related to my volunteer SEO efforts. The site had decent traffic, especially to some speciality interior sections. So the trick was to redirect all the site's pages to a special explanation page.

I tried several mod_rewrite rules, but they all had some problems. Most of the solutions caused an infinite loop. Then I called my great programming partner Aerik for assistance. He helped me customize a mod_rewrite rule that redirected every page in the site (regardless of their file extension) to the special explanation page.

Then I encounter a problem with the special page, when it lost the stylesheet and images, because they were redirected too. I added a few more exclude rules (! means not) for css, jpg and gif files. That did the trick.

Here's the code to redirect all site pages to one with mod_rewrite:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index-special\.php$
RewriteCond %{REQUEST_URI} !^(.*)\.css$
RewriteCond %{REQUEST_URI} !^(.*)\.jpg$
RewriteCond %{REQUEST_URI} !^(.*)\.gif$
RewriteRule ^(.*)$ http://www.domain.com/index-special.php [L,NC,R=301]

Don't forget to check your redirects with a http header checker to make sure they are doing what you want them to do.

I've been really busy with a couple of large client projects recently, but I have posted a few updates to my free web directory reviews.

0 Comments:

 

Post a Comment

Links to this post:

Create a Link

<< Home