Redirect One Page
The Redirect directive from mod_alias needs no rewrite engine. Put it in the .htaccess file in the site's document root:
Redirect 301 /old-page.html https://example.com/new-page.html
The first path is relative to the site root and starts with a slash; the target is a full address. Redirect matches the beginning of the path in whole segments, so Redirect 301 /docs https://example.com/manual also sends /docs/install.html to /manual/install.html. How the file itself works is explained in the .htaccess file.
Test with 302 First
Browsers cache a 301 redirect, sometimes for a long time, so a wrong rule keeps sending visitors to the wrong place after you fix it. Write new rules with 302, test them, and change them to 301 once they work. The difference is explained in 302 Found. Test from the command line, where nothing is cached:
curl -I http://example.com/old-page.html
Look for the status line and the Location header. Add -L to follow the whole chain.
Redirect a Directory with RedirectMatch
RedirectMatch takes a regular expression:
RedirectMatch 301 ^/blog/(.*)$ https://example.com/news/$1
Everything after /blog/ is captured and appended to the new path.
HTTP to HTTPS
Host and protocol redirects need mod_rewrite. Make sure the certificate works on HTTPS before you add this, otherwise visitors land on an error page:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
If the server sits behind a proxy or CDN that connects over HTTP, %{HTTPS} is always off and this rule loops; see ERR_TOO_MANY_REDIRECTS. A certificate for Apache is covered in Certbot for Apache.
www to Non-www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
In .htaccess the path that RewriteRule sees has no leading slash, which is why the target adds one. For the opposite direction, match ^example\.com$ and redirect to https://www.example.com/$1. To send a visitor through one redirect instead of two in a row, use one rule for both cases in place of the two rules above:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
Old Domain to New Domain
In the .htaccess of the old site, with both names pointing to the server:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.net$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
Every path keeps its place on the new domain. Keep the old domain registered and the redirect in place for a long time, since links and search results point to it.
The Flags in RewriteRule
| Flag | Meaning |
|---|---|
R=301 | Permanent redirect |
R=302 | Temporary redirect |
L | Stop processing rules here |
NC | Ignore case |
QSD | Drop the query string |
The query string is kept by default, with RewriteRule and with Redirect. More rule patterns are in Apache mod_rewrite.
When a Rule Does Nothing
- AllowOverride: Apache reads
.htaccessonly where the virtual host allows it.Redirectand rewrite rules need at leastAllowOverride FileInfofor the document root, and rewrite rules also needOptions FollowSymLinksorSymLinksIfOwnerMatchthere. See Apache virtual hosts. - mod_rewrite not loaded: on Debian and Ubuntu enable it with
sudo a2enmod rewrite, then runsudo apachectl configtestand reload Apache. - Mixed modules: rewrite rules and
Redirectlines are processed by different modules. Use one kind per file to keep the order predictable. - Nginx: Nginx ignores
.htaccess. See Nginx redirect HTTP to HTTPS.