how to implement an HTTP redirect (301) for an URL

HTTP Redirect or URL Redirect is a technique used by the web servers to serve the same and identical content for two or more URL addresses. This allows the users to use several different URLs knowingly or unknowingly but the content that is delivered is exactly identical and transparent to the user. There are several cases where this is useful.

As a general rule of thumb, you should minimize the use of HTTP redirects as it can negatively affect the time taken to an URL or page. In spite of its negative implications for SEO in terms of page load times and the extra HTTP request that is necessitated, there are many use cases where using the redirects is the only solution. Many a times it is also used for convenience rather than having to change a large number of URL references.

Domain Level Redirection: It is usually a good idea to maintain just one sub-domain for your website and redirect all others to this single domain. For example, for this website, is redirected to This is a good practice to redirect and maintain only one domain for your website.

Website Redesign: Often times a website redesign will involve changes to the URL structure and file names. This will create broken back links if somebody has linked to your site using the old URLs. Most times you want to redirect these old URLs to an equivalent new URL that will provide the users with a better experience than encountering a broken link or 404 error page.

WordPress and Other Blog Platforms: Most blog platforms allow you to access the same resource such as a page, post or image using multiple URLs. These may be similar looking URLs with slightly different formats. For example, WordPress allows you to access the posts using URLs with or without the trailing (ending) slash.

In some ways, an HTTP direct when used correctly allows you to indicate that the usage of multiple URLs is intentional, especially for the search engines. This will allow you to avoid any penalty in terms of SEO for content duplication.

The first thing you need to be aware of is the HTTP status codes that indicate a redirection. Using the status codes correctly will enable the clients, (eg. Web browsers) to interpret and respond correctly to the intended behavior. Most status code in the 300s refers to redirection of some sorts, except 304 and 305. The following are the status codes that indicate redirection:

301 – Permanent Redirection: This denotes that the URL has permanently relocated to another URL. The user or web agents with abilities to edit the offending link should edit the link and replace it with the new URL.

302 – Temporary Redirection: The URL has been found and it temporarily resides at another URL location. This could change in the future and there is no need to edit the link at this time. The 307 status code has very similar feature.

303 – Other: This is not compatible with all web agents, especially older ones and sometimes work pretty similar to the above 302 code. This allows the server to redirect a POST request script to another resource and specify that the GET method be used.

The most used status code is 301 and is the prime subject of this post. We will look at various ways in which the 301 redirection can be implemented in different cases. We will differentiate these implementations into two main types: client side and server side.

Client Side Redirection

This type of redirection requires some explicit action on the client side, which is the web browser. The client will need to parse and identify the redirected URL and then issue another HTTP request to the new URL in order to load the page. Depending on the limitations of the web server, sometimes this might be the only way to implement a redirection. Server side redirection is preferred over client side if and when possible.

All Web languages provide a mechanism to implement this, we will take a look at how to implement this in some of the commonly used languages.


It is implemented using the header(…) method. The following code effectively adds an HTTP header that denotes that the URL has moved permanently to another URL which is denoted by the Location field.

header("HTTP/1.1 301 Moved Permanently");  


The other very popular web language is Javascript. As javascript executes in the client, you can just reload the page to another URL without specifying even the HTTP status code.


Meta tags in HTML

Along the lines of Javascript, you can use the meta tag in HTML head, to specify the new URL. The web browser will refresh the page with the new URL.

meta http-equiv="refresh" content="0;url=""


There are some plugins available that does redirection for you if you are on a blogging platform like WordPress. There are several different ones that you can find at the WordPress site.


Server Side Redirection

This involves modifying the web server configuration files, so as to send an appropriate HTTP status code and serve content from another resource using server side rewrite rules. As Apache WebServer is one of the most widely used web servers, we will look at how to implement this in Apache server as an example. You should be able to do this with other web servers as well.

The configuration file that you need to change in Apache is the .htaccess file that is located at the root of your webserver. There are two ways to implement this redirect in .htaccess file : mod_alias and mod_rewrite.


This is a straight forward redirect that indicates to the browser that the URL has moved and it should fetch the content from the new URL. Even though, I listed this as a server side redirect it can be classified more as a client side redirection because the browser (or web client) is involved in the process.

You can add the following code in your .htaccess file to do a 301 redirect.

Redirect 301 /old/oldpage.htm


This implementation uses the Apache’s rewrite engine. You will need to turn the rewrite engine on and then specify a set of rules in order to serve the new content. This is the most preferred way of HTTP rewrite because it eliminates the need for another HTTP request and the redirection happens entirely on the server.

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^ [nc]
rewriterule ^(.*)$$1 [r=301,nc]

The Apache rewrite engine gives many options and many rules that allows you to rewrite your URLs based on many varied conditions.