How can I redirect and rewrite my URLs with an .htaccess file?


Creating an .htaccess file on your DreamHost web server

View the following article for instructions on how to create an .htaccess file on your web server:

If the file already exists, view the following articles for instructions on how to update it (depending on if you're using an FTP client or SSH):

What do I change in the following examples?

The examples below can be entered into your .htaccess file exactly as shown. Just make sure to adjust the actual path to the file so the .htaccess file knows where it's located. Also, if you see the domain, change this to your own domain name.

Forcing or removing www in the URL

Forcing www in the URL

RewriteEngine On
RewriteCond %{HTTP_HOST} ^
RewriteRule (.*)$1 [R=301,L]

Removing www in the URL

RewriteEngine On
RewriteCond %{HTTP_HOST} ^
RewriteRule (.*)$1 [R=301,L]

Redirecting all URLs to a new site

The following two examples redirect all URLs on your current website to a second website. This is useful after you have migrated your site to a new domain name.

Option #1

Redirect 301 /

Option #2

In this example, your old site named would redirect while preserving all URLs to a new site named

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^$ [OR]
RewriteCond %{HTTP_HOST} ^$
RewriteRule (.*)$$1 [R=301,L]

Redirecting a single URL

Using Redirect in an .htaccess file enables you to redirect users from an old page to a new page without having to keep the old page. For example, if you use index.html as your index file and then later rename index.html to home.html, you could set up a redirect to send users from index.html to home.html. For example:

Redirect to a local site file

Redirect /path/to/old/file/old.html /path/to/new/file/new.html

Redirect to an external site file

Redirect /path/to/old/file/old.html

The first path

The first path to the old file must be a local UNIX path, NOT the full path. So, if the .htaccess file is in the directory /, you would not include /home/username/ in the local UNIX path. The first / represents the directory. If the old file was in that directory, you would follow the / with the old file name.

The second path

The second path to the new file can be a local UNIX path, but can also be a full URL to link to a page on a different server or the same server.

Examples of redirects

Redirecting from a directory to an HTML file

RedirectMatch 301 ^/blog/about /blog/about.html

Redirecting from an index.html file to a different directory

Redirect /index.html /new/

Redirecting from index.html to default.html

Redirect /index.html /default.html

Redirecting a local /private directory to another site's private directory

Redirect /private/

Loading a .gif file from a different site

Make sure the other site is something you own. You should never hotlink files from other websites.

Redirect /img/logo.gif

Using Regular Expressions

If you want to use a Regular Expression to redirect something, use the RedirectMatch directive:

RedirectMatch "^/oldfile\.html/?$" ""

Redirecting error messages

You can also redirect 404 errors. Instead of throwing a 404 page, this redirects to the homepage of the website.

ErrorDocument 404

Redirecting an old directory to new directory

This redirects files in a old directory (/blog/archives) to a new directory (/archives). The file must exist in the new directory to function.

RewriteRule ^blog/archives/(.*)$ /newarchives/$1 [R=301,NC,L]

Redirecting an old directory to the home directory

This redirects files in a old directory (/blog/archives) to the home directory.

  • If you add a specific file ( to the end, it must exist in the home directory to load.
  • If only the subdirectory is visited, the home directory loads. If you added an index file (such as index.html), it will load.
RewriteRule ^blog/archives/(.*)$ /$1 [R=301,NC,L]

Redirecting non-existing pages to index.php

If a visitor attempts to access a page that doesn't exist, they are presented with a 404 error. You can instead redirect any request to a non-existing page to your index.php file (or any index file) by adding the following code in your .htaccess:

Options +SymLinksIfOwnerMatch 
RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

If your index page isn't index.php, just change the last line to your actual index file. Then the visitor is redirected back to your home page.

Automatically loading a subdirectory

This example redirects the ROOT domain's URL to any subdirectory. In this example, it automatically loads

RewriteEngine on
RewriteRule ^$ /subdir1/ [L]

Rewriting a URL

This example rewrites a URL to another URL. This rewrites to

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([0-9]+).html /abc.php?id=$1 [QSA,L]

The following explains the rules above:

allows any digit, and only any digit, 1 or more times.
allows any lowercase letter, plus “-” for word separation, 0 or more times. If you want it to support uppercase too, use “([a-zA-Z-]*). For example:
RewriteRule ^place/([a-zA-Z-]*).html /place/abc.php?id=$1 [QSA,L]
appends this to your internal scripting query string, and makes it the Last rewrite rule executed.

After using this method, you can retrieve the webpage with either address type. This is handy for retro-fitting a website that was not designed with mod_rewrite in mind. This is good because it does not destroy any bookmarks saved on users computers.

View the following link for more information and examples about mod-rewrite:

Rewriting non-existing links to index.php

The following redirects all links to files or folders that do not exist to index.php. However, if the file or directory does exist, it loads normally:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Redirecting HTTP to HTTPS for your site

View the following article for instructions on how to force your site to load HTTPS:

See also

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?