Creating URL Redirects
There are two broad methods for creating redirects:
- At the web server level
- At the HTML/programatic level
While there are many ways of achieving URL redirects, the following are the easiest and most common methods that apply to the majority of websites.
Webserver Redirects
htaccess file
In most cases, these URL redirects are set up in the .htaccess file that exists in the same folder as the home page for your site.
If you are setting up a handful of permanent redirects for a small handful of pages on the same site, then you can use the following syntax in the .htaccess file
RewriteEngine On Redirect permanent /page1.html http://example.com/newpage1.html Redirect permanent /folder/page2.html http://example.com/newpage2.html
In the above example, this would generate permanent redirects for:
http://example.com/page1.html -----> http://example.com/newpage1.html http://example.com/folder/page2.html -----> http://example.com/newpage2.html
This type of set up is used when you reorganize pages on your website, and you want to ensure that any links or visitors going to the old URL end up on the new URL instead of a 404 page not found error.
Another common set up is to ensure that the www and non-www versions of your domain, eg:
example.com www.example.com
aren't seen as separate sites. The common practice is to redirect all requests from the www to the non-www version of the domain, or vice-versa.
The redirect rules for this would be:
RewriteEngine On RewriteCond %{HTTP_HOST} ^www.example.com$ RewriteRule ^(.*)$ "http://example.com/$1" [R=301,L]
The RewriteCond entry is set to true is the HTTP Host request is the www version of the domain. If this test is true, the next line is evaluated.
The (.*) captures the actual page being requested and is referenced by $1 in the redirect. This effectively means that any page requested from the www version of the domain is redirected to the same page on the non-www version of the domain.
The R=301 means do a 301 Permanent Redirect and the L means this is the last rule to be evaluated.
The other most common type of set up for redirects is where a site has been moved to a new domain. It is important to set up redirects on the old domain to point to the new domain so both visitors and search engines where the website is.
In most cases, every page on the old site is permanently redirected to the site.
The .htaccess entry on the old site would look like:
RewriteEngine On RewriteRule (.*) http://mynewsite.com/$1 [R=301,L]
HTML/Programatic Redirects
PHP Redirects
If you don't have access to using a .htaccess file, the other popular method is to use a PHP page to generate the redirect.
The PHP should not have any HTML before the HTTP header, otherwise the redirect will not be recognised.
The most simple example of a PHP redirect is:
<?php Header('Location: http://example.com/newpage'); ?>
This will do a 302 redirect from the PHP page to http://example.com/new
If you want to do a PHP 301 permanent redirect, you just need to add the redirect code, ie:
<?php Header('Location: http://example.com/newpage',TRUE,301); ?>
which is just a shorthand way of writing:
<?php Header('HTTP/1.1 301 Moved Permanently'); Header('Location: http://example.com/newpage'); ?>
Meta Refreshes
This method uses a special meta tag in the HTML source to control the redirect. In the early days of the Internet, this was the main method of generating redirects.
The meta refresh should appear within the head section of the HTML source.
An example is:
<meta http-equiv="Refresh" content="0; url=http://www.example.com/" />
The number (0 in this example) refers the the number of seconds to wait before performing the redirect. A zero second delay results in an instant redirect and is the equivalent of doing a 301 permanent redirect.
The other type of use for meta refreshes is to display a message to the visitor before doing the redirect.
For example:
<html> <head> <meta http-equiv="Refresh" content="10; url=http://www.example.com/alert" /> </head> <body> <p>Please note that this page will refresh in 10 seconds.</p> <p>Go direct to <a href="http://www.example.com/alert">the new location </a>.</p> </body> </html>
Note that meta refreshes are not recommended by W3C, and if possible you should use one of the other redirect methods listed above.