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
Permanent permanent 301
Temporary temp 302

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.

  1. Code Sample For Redirecting One Domain To Another:

    The following redirect rule matches every webpage from the to the similar URL of different domain So if there is a webpage with URL, it will be redirected to

    #Redirecting to
    <VirtualHost *:80>
    RedirectMatch 301 (.*)$1

  2. 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

  3. 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 should be redirected to

    Note: Observe that we have used the term permanent instead of the 301 response code in this redirection rule.

    <VirtualHost *:80>
    RedirectMatch permanent (.*)$1

  4. 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

    RedirectMatch 301 ^/reviews/smartphone?/?$

  5. 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/)

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.

  1. Start Internet Services (IIS) Manager.
  2. Click to expand * server name, where server name is the name of the server.
  3. Right-click either the Web site or the folder that you want to redirect, and then click on Properties.
  4. Under When connecting to this resource, the content should come from, click A redirection to a URL.
  5. Type the URL of the destination folder or Web site in the Redirect to box.
  6. 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.

Further references
1. Using Redirect – SEO Best Practices
2. Learn SEO – Duplicate Content and its impact on search results

free searchenabler trial version

Identify & Fix Re-direct Errors.
Try SearchEnabler SEO platform.
No credit card required. Quick setup.

Like The Article? Share With Your Friends !

 Vote on HN

Get blog posts via Email!

Only blog feed, No spam.

Connect With Us !

Add New Comment

Add Comment:

You must be logged in to post a comment.