301 重定向
What is a 301 redirect?
A 301 redirect indicates the permanent moving of a web page from one location to another.
The 301 part refers to the HTTP status code of the redirected page.
Example: blog.ahrefs.com redirects to ahrefs.com/blog
In simple terms, a 301 redirect tells the browser: “This page has moved permanently. This is the new location and we don’t intend on moving it back.” To which the browser responds: “Sure thing! I’ll send the user there right now!”
That’s why if you try to visit blog.ahrefs.com, it won’t happen.
You’ll end up at ahrefs.com/blog instead.
How to do a 301 redirect
There are many ways to do 301 redirects, but the most common method is to edit your site’s .htaccess file.
You’ll find this in your site’s root folder:
Don’t see the file? That means one of two things:
- You don’t have a .htaccess file. Create one using Notepad (Windows) or TextEdit (Mac). Just create a new document and save it as .htaccess. Make sure to remove the standard .txt file extension.
- Your site isn’t running on an Apache web server. This is somewhat technical, but there are different types of web servers. Apache, Windows/IIS, and Nginx are the most common. Only Apache servers use .htaccess. To check that your website runs on Apache, ask your web host.
Here are some snippets of code for adding common types of 301 redirect via .htaccess:
IMPORTANT. These instructions are for Apache web servers only. Read this if your site runs on Nginx, or this if your site runs on Windows/IIS.
Redirect an old page to a new page
1 |
Redirect 301 /old-page.html /new-page.html |
Using WordPress? Remove the need to edit the .htaccess file with the free Redirection plugin.
It makes adding 301 redirects as simple as this:
Redirect an old domain to a new domain
1 |
RewriteEngine on |
2 |
RewriteCond %{HTTP_HOST} ^oldsite.com [NC,OR] |
3 |
RewriteCond %{HTTP_HOST} ^www.oldsite.com [NC] |
4 |
RewriteRule ^(.*)$ newsite.com/$1 [L,R=301,NC] |
There are quite a few ways to do this. I am by no means an expert when it comes to Apache servers and htaccess files. This is the code that has always worked for me. Make sure to test this before implementing on your site.
IMPORTANT! If RewriteEngine on
is already in your .htaccess file, do not repeat it. Just copy the rest of the code. It’s also possible to do this in Cpanel, which may be preferable.
Redirect entire domain from non-www to www (and vice-versa)
Here’s the non-www to www version:
1 |
RewriteEngine on |
2 |
RewriteCond %{HTTP_HOST} ^example.com [NC] |
3 |
RewriteRule ^(.*)$ www.example.com/$1 [L,R=301,NC] |
Here’s the www to non-www version:
1 |
RewriteEngine on |
2 |
RewriteCond %{HTTP_HOST} ^www.example.com [NC] |
3 |
RewriteRule ^(.*)$ example.com/$1 [L,R=301,NC] |
IMPORTANT! The placement and order of code in your htaccess file matters too. You may experience unwanted effects if multiple instructions are placed in the “wrong” order (e.g., redirect chains, etc.). If you’re planning to implement a lot of 301 redirects in your htaccess file, this is something worth looking into.
Redirect entire domain from HTTP to HTTPS
1 |
RewriteEngine On |
2 |
RewriteCond %{HTTPS} off |
3 |
RewriteRule ^(.*)$ %{HTTP_HOST}%{REQUEST_URI} [L,R=301] |
IMPORTANT! You must have an SSL certificate installed on your website for this to work. Otherwise, you’ll get the cautionary “Not secure” message.
Redirect entire domain from non-www to www and HTTP to HTTPS