Overview
You can redirect or rewrite the URLs on your website by adding code to an .htaccess file — redirecting all URLs to a new site, redirecting a single URL, directory, or error page, or rewriting non-existing links to index.php.
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
How do I redirect 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>
How do I redirect 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
How do I redirect 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.
How do I rewrite 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.