Redirect One Page
The Redirect directive from mod_.htaccess file in the site's document root:
Redirect 301 /old-page.html https://example.com/new-page.htmlThe 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.htmlLook 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/$1Everything after /blog/ is captured and appended to the new path.
HTTP to HTTPS
Host and protocol redirects need mod_
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_
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= | Permanent redirect |
R= | 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_
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 withsudo 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.
Updated:
Linux VPS with Full Root Access
MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.
More Guides
No Limits on Service or Support
You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

- 100% customer satisfaction goal
- Our own hardware
- 24/7 ticket and email support
- 4 countries with locations