We live in a world of constant change and with every new trend coming being introduced into the markets, many developers or companies try to include that on their website. These changes in a website can either involve deleting pages or often being moved from one domain to another. This process is usually called Redirection where one URL is forwarded to a different URL. However, it is not as easy as it sounds and it is very crucial to handle these redirects correctly in order to avoid losing rankings and help search engines understand the changes you have done. In other words, redirecting helps send both users as well as search engines to a different URL from the one they originally requested. Some of the most commonly used redirects include 301, 302, 307and 404. Here we shall discuss these exclusively.
301: Moved Permanently
This redirect is termed as a permanent redirect that passes link equity of 90-99% to the redirected page. It is considered a well-known redirect that tells the client or a new user to use the new URL resource for future requests that were changed from the original location. So, when the search engines observe a 301 redirect, they automatically pass the old page’s ranking to the new one. 301 is the best method for implementing redirects on a website in most instances. Since 301 is a permanent redirect, you must be very careful before making a change because if you decide to change your mind later, your old URL will not rank anymore. So, you must remember that you cannot undo 301 as it will not help you get the old page back to its previous ranking position.
404: Not Found
This redirect will often happen when you are lazy to investigate all of your 404 URLs and map them to the appropriate landing page. If you have too many pages like this, you should consider creating beautiful 404 pages and engage users to further browse something other than what they were looking for by displaying a search option.
302: Found
This redirect implies that a resource any user or client is looking for is found on a URL in the HTTP 1.1 version but was temporarily moved in HTTP 1.0. However, there are some cases that have indicated that 301s and 302s work similarly, and so it is suggested to use 301 redirects while permanently redirecting URLs. HyperText Transfer Protocol (HTTP) is a protocol that the internet runs on and often dictates how URLs work. It has two major versions namely 1.0 and 1.1. In the first version, 302 redirects are referred to the status code ‘Moved Temporarily’ which is changed in version 1.1 meaning ‘Found.’ Even if in most cases, 302 and 307 redirects will be treated the same, a 302 status code doesn’t necessarily mean the client must follow a redirect without it is being considered an error if it decides to stay there. On the other hand, some old or buggy clients may change the method which may cause unexpected behavior with a 302 status code.
307: Temporary Redirect
A 307 redirect for SEO means the client must follow a redirect but search engines should not update their links in the SERPs to the new, temporary page. The 307 redirects in contrary to a 301 redirect do not pass PageRank from the original resource to the new one. This is mainly because 307 redirect is the HTTP 1.1’s successor of the 302 redirects. It is generally best to use a 302 redirect for content that has been temporarily moved since it’s essentially impossible to determine whether or not the search engines have identified a page as compatible or not. You can use either 302 or 307 but I would recommend 307 personally.
Apart from these redirect instances you may handle redirect via server config files .htaccess on Apache, example.conf file on Nginx or via plugins if you are using WordPress. They usually have the same syntax for writing redirect rules in all instances. But, they might differ with commands exclusively used in configuration files. For example, redirect on Apache will look appear as:
Options +FollowSymlinks
RewriteEngine on
RedirectMatch 301 ^/oldfolder/ /newfolder/
While on Nginx servers, it will appear as:
rewrite ^/oldfolder/ /newfolder/ permanent;
Since Apache is the most widely spread server type, you need to make sure mod_rewrite and mod_alias modules on your server are enabled. You also need to make sure that the .htaccess file has these two lines:
Options+FollowSymlinks RewriteEngine on
Redirect a Single URL
There are some common and widely used different types of redirects that are used during deleting pages or changing page URLs. For instance, if you change URL from /old-page/ to /new-page/,then, the redirect rule would be:
RewriteRule ^old-page(/?|/.*)$ /new-page/ [R=301,L]
OR
RedirectMatch 301 ^/old-page(/?|/.*)$ /new-page/
You will see that the first one uses Apache mod_rewrite module while the second uses mod_alias but it can be done using both methods. The regular expression “^” means URL must start with “/old-page” and (/?|/.*)$ indicates that anything that follows “/old-page/” with slash “/” or without exact match must be redirected to /new-page/.You can also use (.*) i.e., ^/old-page(.*), but the problem is the availability of another page with similar URL like /old-page-other/, will also get redirected when we only want to redirect /old-page/. If we use redirect in the following form, it will redirect any variation of the page URL to a new one without regular expressions then all URLs that had UTM query string.
Redirect 301 /old-page/ /new-page/
Redirect All Except
Let’s say you have bunch of URLs like /category/old-subcategory-1/, /category/old-subcategory-2/, /category/final-subcategory/ and want to merge all subcategories into /category/final-subcategory/. Here, you need the “all except” rule that will redirect all under /category/ on the third line except if it is /category/final-subcategory/ on the fourth line.
RewriteCond %{REQUEST_URI} !/category/final-subcategory/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(category/). /category/final-subcategory/ [R=301,L]
Directory Change
In case you want to do a category restructuring and move everything under the old directory to the new one, you can use the rule below.
RewriteRule ^old-directory$ /new-directory/ [R=301,NC,L]
RewriteRule ^old-directory/(.*)$ /new-directory/$1 [R=301,NC,L]
Remove a Word from URL
Let us assume that you have 100 URLs on your website with city name “chicago” and want to remove it. For Example, if the URL is http://yourwebiste.com/example-chicago-event/, the redirect rule would be:
RewriteRule ^(.*)-chicago-(.*) http://%{SERVER_NAME}/$1-$2 [NC,R=301,L]
If the example URL is in the form http:// yourwebiste.com/example/chicago/event/, then redirect will be:
RewriteRule ^(.*)/chicago/(.*) http://%{SERVER_NAME}/$1/$2 [NC,R=301,L]
Canonicalization
Canonical URLs are the most important part of SEO and their absence might endanger your website with duplicate content issues. This is because search engines treat URLs with “www” and “non-www” versions as different pages with the same content. Therefore, it becomes mandatory to make sure you run your websites with only one version you choose. But if you choose to run your website with “www” version, you can use this rule:
RewriteCond %{HTTP_HOST} ^yourwebsite\.com [NC]
RewriteRule ^(.*)$ http://www.yourwebsite.com/$1 [L,R=301]
For a “non-www” version:
RewriteCond %{HTTP_HOST} ^www\.yourwebsite\.com [NC]
RewriteRule ^(.*)$ http://yourwebsite.com/$1 [L,R=301]
Also, trailing slash is also a part of canonicalization since URLs with a slash at the end or without are treated distinctively.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]
This will make sure /example-page is redirected to /example-page/. You may choose to remove the slash instead of adding and then you will need the other rule as stated below:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
HTTP to HTTPS Redirect
Google’s initiative to encourage website owners to use SSL had lead to the migration to HTTPS as one of the commonly used redirects that almost every website has. The rule below can be used to force HTTPS on every website.
RewriteCond %{HTTP_HOST} ^yourwebsite\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.yourwebsite\.com [NC]
RewriteRule ^(.*)$ https://www.yourwebsite.com/$1 [L,R=301,NC]