weseodesign.com logo - Website Development, Search Engine Optimization & Search Engine Marketing

.htaccess and mod_rewrite (redirects) Explained

Quick copy and paste code snippets:

Updated: 03/05/06

Jump to:
Rewrite non www domain to www.domain
Rewrite www.domain to domain
Rewrite one domain to another
Mod_rewrite details
FrontPage and Mod_rewrite: problems and solution
Redirect a page (using redirect)
Rewriting to exclude https
Redirecting all your pages to a special page

One of the most common reasons for using .htaccess and mod_rewrite is to redirect you domain (non www version) to your www.domain (or vice versa). This is generally implemented to consolidate the versions of your domain name, in order to prevent PageRank/link pop splitting and possible duplication penalties with the search engines. These examples will redirect the domain and all the files/directories below the domain.

You may also want to redirect a second domain name to you main domain. You need to do this in addition to "parking your domain" via server's control panel and/or DNS records.

If you want to redirect an individual page to another one use this method.

The .htaccess file can be located in any directory, but the one we are dealing with is in your public www root, htdocs or public_html directory (or similar name).

Cautionary Note: Do not use these examples if your site serves https secure pages. These rewrite rules will redirect the https address to http and will probably prevent your secure functions from executing properly. Use this special example to exclude https from redirecting.

Here is an example of a typical mod_rewrite statement (see details below for additional information). This will 301 redirect http://yourdomain.com to http://www.yourdomain.com (the domain and all pages and directories).

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

Here is the example in reverse. This will 301 redirect http://www.yourdomain.com to http://yourdomain.com:

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

Here is an example of redirecting a "parked" domain. This will 301 redirect http://www.yourotherdomain.com and http://yourotherdomain.com (or .net, .org, etc.) to http://www.yourdomain.com:

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

Details:

  1. This method only works on Linux/Apache servers (and related 'nix flavors).
  2. mod_rewrite (RewriteEngine) needs to be enabled on your server
  3. If you use more than one rule set, you only need to use "Options +FollowSymLinks" and "RewriteEngine on" once.
  4. If you use FrontPage Extensions, using this will break certain functions such as remembering your login and FrontPage forms (see work around below).
  5. .htaccess files must be edited in a text editor using "Unix" mode and uploaded via ftp in the "ASCII" mode.  This is necessary to preserve the "Unix line ender".  Do not use a regular DOS text editor (like Windows Notepad) unless it has a "Unix" mode to create or edit your .htaccess files. UltraEdit 32 * (free trial) and PSPad (freeware) both work well.
  6. Be sure to test the rewrite in your browser and with an http header checker (make sure it returns a 301 redirect status code).

How to test if mod_rewrite is enabled:

Create a test directory on your server (like www.yourdomain.com/test/) and make a "ASCII" text file named .htaccess (you may have to name it something else without a "dot" preceding the filename and rename it on the server - this is a hidden file). Add the following two lines, save the file and ftp (via "ASCII" mode) the file to your server /test/ directory:

Options +FollowSymLinks
RewriteEngine on

Now enter the test directory path into your browser (http://www.yourdomain.com/test/):

If you don't get an error, mod_rewrite is enabled. If you get a 500 server error, mod_rewrite is not installed or enabled on your server (contact your server admin/web host for assistance)

If your web host says you can't use mod_rewrite or won't allow you to edit .htaccess, dump them and try my favorite web hosting company Lunarpages *. They are the greatest!

FrontPage Extensions and .htaccess/mod_rewrite:

Most people say you can't use FrontPage Extensions with mod_rewrite in your .htaccess. This was initially thought to be true, but there is now a work around:

Use the code example above and make the following modification to these .htaccess files in the following locations:

Replace "Options None" with "Options +FollowSymLinks" in the .htaccess files in these directories:

Your public html or www root/.htaccess
_vti_bin/.htaccess
_vti_bin/_vti_adm/.htaccess
_vti_bin/_vti_aut/.htaccess

Originally posted at WMW: Apache mod_rewrite and FrontPage extensions: Post #43 & 44 from Bumpski, chopin2256, and steveb (thanks guys & thanks to Rhoda Schueller for finding this for me).

Update: 12/11/05 The changed/unchanged/conflict/etc. status column in FrontPage doesn't always work correctly after a refresh with this "hack" in place - not a big deal for me.

Redirect one page to another (without mod_rewrite):

redirect 301 /page1.htm http://www.domain.com/new-page1.htm
#(you can use permanent instead of 301 also - if left out it becomes a 302 redirect)

#OR

RedirectMatch permanent ^/page1.htm$ http://www.domain.com/new-page1.htm

You can also use this to redirect to a page on another domain (domain2.com) by placing a similar redirect in the originating sites .htaccess file (domain.com):

redirect 301 /page1.htm http://www.domain2.com/new-page1.htm
#(you can use permanent instead of 301 also - if left out it becomes a 302 redirect)

#OR

RedirectMatch permanent ^/page1.htm$ http://www.domain2.com/new-page1.htm

Exclude https from redirecting:

If you utilize SSL secure functions on your site (ie: for a shopping cart), the examples above will redirect an https URLs to http. This will generally prevent your secure application from functioning. Here is an example to only rewrites http requests (usually on port 80):

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SERVER_PORT} ^80$
RewriteCond %{HTTP_HOST} ^yourdomain\.com$ [NC]
RewriteRule .* http://www.yourdomain.com%{REQUEST_URI} [QSA,R=301,L]

Special thanks to Richard at Doriat mod_rewrite forums for the solution to this difficult problem.

This alternate solution may work with some server configurations, but it didn't work for me:

RewriteEngine On
RewriteCond %{HTTPS} ^off$
RewriteCond %{HTTP_HOST} ^yourdomain\.com$ [NC]
RewriteRule .* http://www.yourdomain.com%{REQUEST_URI} [QSA,R=301,L]

Redirecting all your pages to one:

This special rewrite is useful when a you need to close a site (permanently or for maintenance) and send the traffic from all the pages to a special page that explains the current situation. This example excludes stylesheets, jpg and gif images from the redirect, so you can still use our "look and feel" on the special pages. See my Mod_rewite Redirects All Site Pages To One post for some more on this one.

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]

Resources and References:

Doriat mod_rewrite forum - they have numerous great examples of various solutions to mod_rewrite situations. They will also help to find a solution for your specific need.

Apache URL Rewriting guide - an excellent, though highly technical guide to mod_rewrite.

mod_rewrite Cheat Sheet - a .png image with most of the syntax listed (print it out)

 

* Disclaimer:  We have an affiliate relationship with some of the sites, products and services listed here.  All such sites, products or services are denoted with *.  We only recommend products because they are, in our opinion, one of the best available in their market.  We use all the sites, products and services we recommend.


Website Development Home

Website Resources

Free Directories

Paid Directories

SEO Tool Chest

About Us

Contact Us

Site Map

blog