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

 

Overview

This article explains how to add code to an .htaccess file to redirect or rewrite URLs on your website.

Creating an .htaccess file on your DreamHost web server

See this article for instructions on how to create an .htaccess file on your web server. 

Redirecting

The examples in this section redirect specific directories or files to another location.

Common redirects

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 all old URLs to the new homepage

Redirect 301 / https://example.com/

Option 2 — Redirect all old URLs to the new URL 

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

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

Redirecting a single URL

These examples redirect a single file to a local or external file.

Note the following:

  • The first path: This must be a local path on the server, NOT the full URL path. So, if the .htaccess file is in the directory /example.com, you would not include /home/username/example.com in the local UNIX path. The first / represents the example.com directory. If the old file was in that directory, you would follow the / with the old file name.
  • The second path: This can be a local UNIX path or a full URL linking to a page on a different server or the same server.

Redirect to a local site file

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

Redirect to an external site file

Redirect /path/to/file/old.html https://www.example.com/new/file/new.html

Redirecting error messages

Use ErrorDocument to redirect error codes. This example redirects a 404 error to the homepage of the website.

ErrorDocument 404 https://example.com/

Redirect directories

Redirecting from a directory to a file

This redirects the directory /blog/about to the about.html file.

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

Redirecting a local directory to another site's directory

This redirects the local directory named private to another site's private directory.

Redirect /private/ https://www.example.com/private/

Redirecting all files in an old directory to a new directory

This redirects all files in an old directory named archives to a new directory named newarchives.

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

Automatically loading a subdirectory

This example redirects the domain's URL to the subdirectory named blog:

RewriteEngine on
RewriteRule ^$ /blog/ [L]

Redirect files

Redirecting an index.html to a different directory

Redirect /index.html /directory/

Redirecting index.html to default.html

Redirect /index.html /default.html

 

Rewriting

The examples in this section rewrite URLs to a specific location.

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]
</IfModule>

Redirecting HTTP to HTTPS for your site

See this article for instructions on how to force your site to load HTTPS:

Forcing or removing www

See this article for instructions on forcing www to be added or removed from a URL.

See also

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?