Good Apache mod_rewrite Examples
Written by Tony   
Thursday, 24 April 2008

Ok, after a lot of research, trial and error, and pulling out of my hair, I found some good mod_rewrite examples that actually seem to work. I tried a bunch that didn't. Here goes, but first some prereqs:

  1. Your Apache server needs mod_rewrite enabled. Check with your host or server admin.
  2. My examples here are in .htaccess files. What's in the type code below is all you need in that file - unless you already have something in that file, then you might want to keep that stuff there - up to you.
  3. I got these examples from the sources mentioned below.

Require the "www" in a domain (you want to do this so that Google or other search engines don't think there are 2 versions of your site out there - the www version (like "www.yourdomain.com") and the non-www version (just "yourdomain.com):

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.yourdomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L]

You need to, of course, change this to your real domain name.

If you don't want to do that, just use this (better) example instead, where you don't have to hard-code your domain name - it even takes care of sub-folders better, I've found:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www..*
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^([^.]*).(com|com/)
RewriteRule ^.*$ http://www.%1.%2%{REQUEST_URI} [R=301,L]

Another good one is how to make sure other sites don't link to your images from their site (I've had reports of this not working perfectly):

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?yourdomain.com/.*$ [NC]
RewriteRule \.(gif|jpg|swf|flv|png)$ /feed/ [R=302,L]

You need to hard-code your domain name into this one. This .htaccess file may need to be in your images directory to work.

Redirect One Domain to Another

And here's one I'm adding since it's so useful... redirecting everything from an old domain to a new domain (not just the home page) so that you don't lose traffic from bookmarks, links and search engine results. This one's important to have on hand:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.yourolddomain\.com [nc]
RewriteRule (.*) http://www.yournewdomain.com/$1 [R=301,L]

WordPress No index.php in URL

Another great one to add for WordPress blogs or Joomla sites. Since you don't want an "index.php" file to have duplicate content as the root / default page of your website (for SEO reasons / how Google sees your website), you want .htaccess to redirect "index.php" to just the domain name. Here's that one:

Options +FollowSymLinks
DirectoryIndex index.php

RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.yourdomain.com/ [R=301,L]

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

Catch All Redirect to Single Page

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com [nc]
RewriteRule (.*) http://www.newdomain.com/ [R=301,L]

RewriteCond %{HTTP_HOST} ^olddomain\.com [nc]
RewriteRule (.*) http://www.newdomain.com/ [R=301,L]

And there's even more here (some of my sources):

BTW, does anyone else hate those forum pages where you think you'll find the answer but then the people in the forum don't understand the question (when you do) and they go on and on and then some idiot either says read the manual or else points you to a page that's not up anymore? What is it with geeks anyway? Nobody bothers to post the answer or a page with just the question and answer on it - which would be really helpful.


Last Updated ( Wednesday, 07 December 2011 )
 
Tony Herman

Follow Me on Twitter

Follow Me on Twitter

MadisonPhotoArt.com

Check out my photography for sale - mainly photos about Madison, WI.