mod_rewrite primer

I’ve had to write a number of .htaccess files over the past week or so. My uses of mod_rewrite are simple enough, but I thought to post here since there in case anybody just wants to accomplish something quickly. Ask your server administrator to set the “AllowOverride” property to “FileInfo” or “All”, so as to enable .htaccess overrides.


Permanent (301) redirect everything:
RewriteEngine On
RewriteRule ^(.*)$ http://waynekhan.com/v2/ [R=301,L]

Permanent (301) redirect some_page.htm (note to prefix your URL with “^/” and escape fullstops (.) with “\”):


RewriteEngine On
RewriteCond %{REQUEST_URI} ^/some_page\.htm
RewriteRule ^(.*)$ http://waynekhan.com/v2/some/page [R=301,L]

That’s all for now, folks!

Advertisements