>

Fun with mod_rewrite…

Today I had to create some specific/generic rewrite rules. The idea was to allow “motion.com,”www.motion.com,” “www.motion.com/index.html,” and “motion.com/index.html” all go to an index (domain for sale) page. However, let everything else forward to “learninginmotion.com.” So URLs like “www.motion.com/products/index.html” would forward to “www.learninginmotion.com/products/index.html.” The resulting rule was:

RewriteCond %{REQUEST_URI} !^/index.html$
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^(.*)$ "http\:\/\/www\.learninginmotion\.com/$1" [R=302,L]

That worked almost perfectly, except for one oversight. The secure store uses secure105.inmotionhosting.com/store/… and that was also getting redirected to learninginmotion.com. Also, the other add-on domains were messed up. To fix it, one line was added, so now it looks like:

RewriteCond %{REQUEST_URI} !^/index.html$
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{HTTP_HOST} !^secure105.inmotionhosting.com$
RewriteCond %{HTTP_HOST} !^voyagesthroughtime.org$
RewriteCond %{HTTP_HOST} !^knowledgeforum.com$
RewriteRule ^(.*)$ "http\:\/\/www\.learninginmotion\.com/$1" [R=302,L]

In the process I discovered another cool little gem. Instead of server up a 404 for non existing pages and directories, you can just redirect them to index.html using:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.html [L]

Silly to put this here but, just in case, we shouldn’t forget to start things off by enabling rewrites using:

RewriteEngine On
banner ad

Comments are closed.