Redirects and .htaccess
A redirect sends visitors and search engines from one address to another, for example from an old page to its new location or from http:// to https://. This guide explains which kind of redirect to use and how to write redirect rules in an .htaccess file, for website owners who manage their own site on web hosting.
301 or 302
When a browser asks for an address that redirects, the server answers with a status code and the new address.
| Code | Meaning | Use it when |
|---|---|---|
| 301 Moved Permanently | The address has changed for good | A page moved, you changed domain, or you force HTTPS or one version of www |
| 302 Found (temporary) | Go elsewhere for now; the original address stays valid | Maintenance pages, short campaigns or tests |
Search engines treat a 301 as a signal to replace the old address with the new one. Browsers may also remember a 301 for a long time, so test new rules with a 302 first and switch to 301 once they work.
What .htaccess is
.htaccess is a configuration file read by the Apache web server, and by servers that are compatible with its rules, such as LiteSpeed. It sits in a website folder, usually the document root, and its rules apply to that folder and everything inside it. The name starts with a dot, so file transfer apps may hide it until you turn on hidden files.
The redirect rules below use mod_rewrite, an Apache module that rewrites and redirects addresses based on patterns.
Servers running nginx ignore .htaccess files. On nginx, redirects go in the server's own configuration, which you can change on a VPS or cloud server but usually not on shared hosting. If your rules have no effect, check with your host which web server your site uses.
Before you edit
- Download a copy of the current
.htaccessfile and keep it on your computer. - Check whether your website app already writes rules to the file. WordPress, for example, keeps its own block between
# BEGIN WordPressand# END WordPress. Put your rules above that block, never inside it. - Make one change at a time, save, and test in a private browser window.
A single mistake in .htaccess usually makes every page show a 500 Internal Server Error. If that happens, upload the copy you saved, or remove the lines you just added, and the site comes back.
Redirect HTTP to HTTPS
Once your site has a working SSL certificate, send all visitors to the secure address:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond sets a condition (the request is not using HTTPS) and RewriteRule does the redirect. R=301 makes it permanent and L stops further rules from running for this request.
If your site sits behind a proxy or CDN that handles HTTPS for it, this condition can cause a redirect loop. In that case, use the HTTPS setting of the proxy or CDN, or follow your host's guidance.
Choose www or non-www
Pick one version of your address and redirect the other to it, so visitors and search engines see a single site.
To send www.example.com to example.com:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
To send example.com to www.example.com:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
NC makes the match ignore upper and lower case. Both names need DNS records and must be covered by your SSL certificate, see Connect a domain to a website. RewriteEngine On only needs to appear once in a file, so if you combine rules, keep a single copy of that line at the top.
Redirect single pages
For one page that moved, Apache's simpler Redirect directive is enough:
Redirect 301 /old-page.html https://example.com/new-page/
To move a whole folder, keeping the rest of each address:
RewriteEngine On
RewriteRule ^blog/(.*)$ https://example.com/news/$1 [R=301,L]
To move an entire site to a new domain, place this in the old domain's .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^(.*)$ https://example.net/$1 [R=301,L]
Test your redirects
-
Open the old address in a private window and check where you end up.
-
Check the status code from a terminal. The
-Ioption shows only the response headers:curl -I http://example.com/old-page.htmlLook for
301or302and aLocation:line with the new address. -
Watch for redirect loops, where the browser reports too many redirects. They usually mean two rules send visitors back and forth, or a rule matches its own destination.
-
Avoid chains, where one redirect leads to another. Point each old address straight at its final destination.
Related guides
- SSL certificates explained
- Connect a domain to a website
- A website that won't load
- PHP versions and settings