Redirect Code & Snippets
Previous article SEO Best Practices for using Website Redirection in the SearchEnabler blog explained about the redirection of webpages. This article gives the procedures and sample code snippets to set up website redirects in correct way. We have mentioned the procedures of redirecting webpages at server level for Microsoft IIS Web Server and Apache Web Server because these are the most used web servers in the world. Let us have a look at them.
1. Code Samples For Webpage Redirection On Apache Web Server
Redirection of webpages in hosted in the Apache Web Server can be done through various methods. The webpage redirection can be set up at Server Configuration Level, Virtual Host Level, Directory Level and through .htaccess file.
Here are the code samples for various kinds of webpage redirection rules at server level. The term RedirectMatch is used when there is a need match the exact regular expressions, while the term Redirect is used when you know the complete path of the old as well as new URL. For permanent redirection, mention the term permanent or response code 301 in the redirection rules. To set up a temporary redirection rule, use the term temp or response code 302.
|Redirection Type||Term||Response Code|
Note: If you do not mention the type of redirection explicitly, temporary redirection is used as default.
You can read all the rules for URL Rewriting On Apache Web Server.
- Code Sample For Redirecting One Domain To Another:
The following redirect rule matches every webpage from the www.example.co.uk to the similar URL of different domain www.anotherexample.com. So if there is a webpage with URL http://www.example.co.uk/index.html, it will be redirected to http://www.anotherexample.com/index.html.
#Redirecting example.co.uk to anotherexample.com
RedirectMatch 301 (.*) http://www.anotherexample.com$1
- Code Sample Of Rewrite Rule For Redirecting One URL To Another URL:
In this redirect rule, the term Redirect is used because we have no intention to match the terms with use of regular expressions which we did in the previous example. It will just redirect the first URL to the second URL as mentioned.
Redirect 301 http://example.com/oldfile.html http://example.com/newfile.html
- Code Sample For Redirecting non-www to www URL:
As we mentioned in our previous article, a problem of duplicate content arises when we do not redirect the URLs without www in them to the URLs with www in them. The following redirection rule defines that every URL such as example.com/news.html should be redirected to http://www.example.com/news.html.
Note: Observe that we have used the term permanent instead of the 301 response code in this redirection rule.
RedirectMatch permanent (.*) http://www.example.com$1
- Code Sample For Redirecting Through Regular Expression:
If you want to match a specific term found anywhere in the URL and redirect to another URL, you can use this redirection rule. In the following example, we are searching the term starting with /reviews/smartphone/ or /reviews/smartphones/ found anywhere in the URL and if matched, redirect it to the URL http://www.example.com/blog/reviews/smartphones/.
RedirectMatch 301 ^/reviews/smartphone?/?$ http://www.example.com/blog/reviews/smartphones/
- Code Sample For Directory Level Redirection:
This redirection rule can be used for redirecting all the webpages from one directory to another. We have redirected all the webpages in the features directory to the new-features directory.
#Redirecting all the webpages in the ‘features’ directory to the ‘new-features’ directory.
RedirectMatch 301 (/features/) http://www.example.com/new-features/
2. Redirection Procedure in Microsoft IIS Web Server
If you are using Microsoft’s IIS to serve your webpages, follow these simple steps to redirect website or webpages to new URLs.
- Start Internet Services (IIS) Manager.
- Click to expand * server name, where server name is the name of the server.
- Right-click either the Web site or the folder that you want to redirect, and then click on Properties.
- Under When connecting to this resource, the content should come from, click A redirection to a URL.
- Type the URL of the destination folder or Web site in the Redirect to box.
- Click OK.
For more information, read How to redirect browser requests in IIS.
There are other possible ways to redirect your webpages such as using page level redirects, but we prefer to use server-level redirects. Server level redirects are preferable because they are faster than page-level redirects as well as transparent to search engines. Being transparent to search engines helps them to pass the ranking factors of the older URL to the new one.
1. Using Redirect – SEO Best Practices
2. Learn SEO – Duplicate Content and its impact on search results