A 301 redirect, also called a “301 Moved Permanently” redirect, is meant for permanently redirecting traffic to a new URL.
Basically, it gives the visitor of the old url the message that it should replace its script or link to the new url. An example of this is when a website has been running on HTTP (without SSL) for years and is now equipped with HTTPS (with SSL). The 301 redirect then indicates to the visitor of the old URL that the link must be changed from http://www.theory7.net to https://www.theory7.net
301 Redirect and SEO
A 301 redirect is the friendliest redirect you can make in terms of SEO. You tell the search engine that the URL you considered to be the old URL is now available at a new URL.
If there are backlinks or other referring websites linked to the old URL, then the search engine such as Google or Bing will also neatly forward this value that comes in at the old URL to the new URL.
Therefore, it is always very important to make sure that you create a 301 redirect for 404 pages, so that the value on SEO and the visitors always show good content on the good URL! So never just take pages offline, but take them offline and make a 301 redirect to another page or relevant content.
301 redirect methods
A 301 redirect can be made in several ways. It differs per hosting package/server which method is easiest for you. We will discuss the most common ones below:
- 301 redirect via .htaccess rule
- 301 redirect via mod_rewrite in .htaccess or Apache Vhost
- 301 redirect with PHP script
- 301 redirect with Nginx
301 redirect through .htaccess rule
A 301 redirect can be made in a so called .htaccess file. This is a file that really starts with a dot and needs to be placed in the same folder as your website files. Often this folder is called public_html, or public or static. This varies by hosting package / server.
Example of 301 redirect via .htaccess
Redirect 301 /path/to/old/url https://www.theory7.net/pad/naar/nieuwe/url
**Another example:**
Redirect 301 /old-page https://www.theory7.net/nieuwe-pagina
301 redirect via mod_rewrite
This method that goes via mod_rewrite is a commonly used method and is also often used in the .htaccess. You can recognize and often see this method of 301 redirect when redirecting visitors or search engines from an old website (domain name) to a new website (domain name).
It is also often used to force everyone to go from http:// to https:// and thus to ensure that everyone goes over the secure connection.
**What is mod_rewrite?
Mod_rewrite is a module that is loaded into Apache. Apache is the software that reads your scripts and html files and then converts them into a visual representation. In short, it turns your written html code into a visible website.
The mod_rewrite module is an option/extra module that is loaded with which you can tell Apache to redirect urls to new urls. Since it is done in Apache while processing your html code it is also very fast!
Example of 301 redirecting via mod_rewrite
RewriteEngine On
rewritecond %{http_host} ^theory7.net
rewriteRule ^(.*) http://www.theory7.net/$1 [R=301,L]
With this example we send all visitors who visit our website via http://theory7.net , so without www in front of it, to the version with www in front of it, namely http://www.theory7.net .
Example Force SSL 301 redirect mod_rewrite
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
SetEnvIf X-Forwarded-Proto "https" HTTPS=on
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
In this example, we indicate to mod_rewrite that if the URL we are visiting is visited over http:// (i.e. without SSL), we would like to redirect it to https:// (i.e. with SSL) and we want to do this via a 301 redirect.
As soon as you use this 301 redirect, every visitor will be forced to a https:// SSL link and from that moment on you will always see a lock in your address bar.
301 redirect via PHP (header function)
It is also possible to make a 301 redirect in PHP. This is the variant I would least recommend, because Apache and PHP first have to read and execute the script you wrote and only then redirect you via a 301 redirect. This is therefore considerably slower than if it goes via .htaccess or via mod_rewrite!
Example 301 redirect via PHP header function
<?php
//First we open the PHP code with <php
//Then we open the header function and set the HTTP status code to 301 moved permanently.
///Then we give the new URL via Location.
header( "Status: 301 Moved Permanently" );
header( "Location: https://www.theory7.net/nieuwe-url" );
exit();
?>
Using the code above, you can simply redirect someone to a new URL via PHP. You can of course customize it to your liking.
The // lines are just for explanation, PHP does nothing with them so you may also remove them if you use the code!
301 redirect via NGINX
It may be that your web hosting provider uses NGINX instead of Apache. This is a different kind of software that works slightly differently than Apache and is often more focused on higher performance and requires less processing power.
Unfortunately NGINX doesn’t have .htaccess or mod_rewrite, so you can’t 301 redirect using those methods with NGINX. Through PHP you can, but there is another option through the config of NGINX.
Through the NGINX vhost config, you can create 301 redirects for various purposes. I will give you some examples that might be useful to you:
301 redirect to a new domain via NGINX
server {
server_name olddomain.net;
rewrite ^/(.*)$ http://nieuwedomeinnaam.net/$1 permanent;
}
In this example, you create a server object in the NGINX config. In that object you specify the old domain names at server_name. This means that the object will be activated in the nginx config as soon as you surf to the old domain name.net.
In this case there is a rewrite line below. There we set up that when you surf to the olddomainname.net you must be redirected to the new domainname.net. This is done via a 301 redirect!
301 redirect old page to new page in NGINX
server {
server_name theory7.net www.theory7.net;
rewrite ^/old-page$ /new-page.html permanently;
}
To 301 redirect an old page url to a new page url, you need yet another code. Above, we again create a server object in the NGINX config.
At server_name we set the domain name theory7.net and www.theory7.net . This means the object will be activated when we visit one of these domain names in the address bar.
Next you will see a redirect line with which we say that /old-page ( and everything after it) must be redirected to /new-page.html. So in this way we easily redirect a specific page via a 301 redirect in NGINX!