There are times where you will need to redirect a URL to another, and considering that there are lots of methods of redirection, and lots of questions about the best method, I wanted to offer a few thoughts on this. Firstly, we need to look at the common reasons for redirection. These are:
Moving to a New Domain
There will be times where your domain name will need to change, and since it will take some time for the search engines to index the new URL and its pages, you will want to ensure that any inbound links to your old website are redirected to your new one.
URL shortening
With dynamic websites, the URL is often long if some form of rewrite is not implemented. In this scenario a URL shortening service is used to provide a shorter (easily remembered) URL that will link to the longer, default version.
Persistent aliases for changing URLs
Sometimes the URL of a page changes even though the content stays the same, a common practice of SEO to optimize a page name for keyword benefit.
Removing referer information
When a link is clicked, the browser sends the HTTP request a referrer which indicates the source of the link. Since sensitive pages may have sensitive URLs a redirection page that performs referrer hiding could be embedded in all external URLs, transforming the page URL. This technique also eliminates other potentially sensitive information from the referrer URL, such as the session ID, and can reduce the chance of phishing by indicating to the end user that they passed a clear gateway to another site.
There are less favorable reasons that URLs are redirected as well, which include:
Manipulating search engines
Not too long back it was a common practice to create “doorway” pages (page created solely for the purpose of redirection) as a means to redirect traffic. This practice is no longer commonly used since it is seen as a blackhat technique, and most search engines are wise to it and will either reduce a page/s ranking or exclude them from a search index.
Satire and criticism
A domain name that conveys one meaning can be redirected to any other web page, sometimes with malicious intent. The website shadyurl.com offers a satirical service that will create an apparently “suspicious and frightening” redirection URL for even benign web-pages.
Manipulating visitors
URL redirection is sometimes used as a part of phishing attacks that confuse visitors about which web site they are visiting. Because modern browsers always show the real URL in the address bar, the threat is lessened. However, redirects can also take you to sites that may attempt to trick them into downloading antivirus software and ironically installing a trojan of some sort instead.
URL redirection methods
With URL redirects, incoming links to an outdated URL can be sent to the correct location. This is often needed when a site has been linked to by others, as most established websites will be. In the same regard, many search engines will take some time before crawling a website, and the URL redirect ensures that one doesn’t receive a “404 page not found” error. By using a “moved permanently” redirect to the new URL, visitors will end up at the correct page, and in the next search engine crawl the new URL should be indexed. There are several techniques to implement a redirect, which I have listed the “common” ones following:
Meta Refresh:
In many cases, a refresh meta tag is the simplest redirect method. However, most designers discourage this approach as I will explain below. The way this works is that you add a metatag to the <head> portion of the page as follows: <meta http-equiv=”refresh” content=”6;url=https://yourdomainname.com/”>
The http-equiv=”refresh” attribute tells the browser that this meta-tag is sending an HTTP command rather than a standard meta-tag (tells the server that the page is going to be reloaded or sent somewhere else). The content=”6″ is the amount of time, in seconds, until the browser should reload the current page (you can put any time value here); this is followed by a semicolon and the url to be redirected to. One of the most common uses of the reload version of the refresh tag is to reload a page that has dynamic content on it. For example: a stock ticker or weather map, and some use them to reload ads.
It is important to note that Meta refresh redirects have been used by spammers to fool search engines, so search engines may remove sites that use a lot of them from their database. It’s better to use a 301 Server Redirect instead. In addition there is a usability concern since a redirect that happens quickly (less than 3 seconds) won’t allow those with older browsers to use the “Back” button. And, refreshing the current page can confuse people, and some may see this as a deceptive tactic and not trust your website.
Manual redirect:
The simplest technique is to ask the visitor to follow a link to the new page, usually providing a simple page with company name and logo, and a bit of text saying something like “Our location has changed, please find us at: <a href=”https://www.yourdomainname.com/”>Our New Website</a>. This method is often used as a fall-back for automatic methods; useful if the visitor’s browser does not support the automatic redirect method.
As noted above, a permanent redirect is generally considered the best method of redirection, though depending on your needs, there are a few different HTTP status codes available.
300 multiple choices (e.g. offer different languages)
301 moved permanently
302 found (originally temporary redirect, but now commonly used to specify redirection for unspecified reason)
303 see other (e.g. for results of cgi-scripts)
307 temporary redirect
All of these status codes require that the URL of the redirect target be given in the Location: header of the HTTP response. This is a sample of an HTTP response that uses the 301 “moved permanently” redirect:
HTTP/1.1 301 Moved Permanently
Location: http://www.yourdomain.com/
Content-Type: text/html
Content-Length: 174
<html>
<head>
<title>Moved</title>
</head>
<body>
<h1>Moved</h1>
<p>This page has moved to <a href=”https://www.yourdomainname.com/”>https://www.yourdomainname.com/</a>.</p>
</body>
</html>
Note that the “content-length” is the number of bytes of data in the body of the request or response. The body is the part that comes after header portion in source code view.
Server-side scripting for redirection:
For CMS websites, a mod-rewrite (applied to the .htaccess file) is the best method of redirection. There are a variety of methods, often dependent on the server platform (Apache, Microsoft) and type of scripting used (PHP, ASP, Perl, etc). For example:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.yourdomain.com$ [NC]
RewriteRule ^(.*)$ https://www.yourdomain2.com/$1 [R,L]
It is important to note that rewrites are used for many things beyond just the redirect of the domain. Common uses are protecting images and files from hotlinking, redirection to other directory, redirect multiple domain name versions (often a non-www to a www. format), and redirect by visitor user agent (browser).
For static websites, one can edit their .htaccess file as well.
Redirect permanent /oldpage.html
https://www.yourdomainname.com/newpage.html
Redirect 301 /oldpage.html
https://www.yourdomainname.com/newpage.html
To redirect a requests for any non-canonical domain name using .htaccess or within a <Directory> section in an Apache config file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([^.:]+.)*oldsite.yourdomainname
.com.?(:[0-9]*)?$ [NC]
RewriteRule ^(.*)$ http://newsite.yourdomainname.net/$1
[R=301,L]
Use of .htaccess for this purpose usually does not require administrative permissions. However, .htaccess can be disabled by your host.
JavaScript redirects:
JavaScript offers several ways to display a different page in the current browser window. Quite frequently, they are used for a redirect. However, there are several reasons to prefer HTTP header or .htaccess redirects over JavaScript redirects: Security considerations, some browsers don’t support JavaScript, some web crawlers don’t execute JavaScript. That being said, here is a JavaScript redirect option that will read the browser used and redirect accordingly (useful if you have different page versions for different browsers):
<script>
if(navigator.userAgent.indexOf(“Firefox”) != -1)
{
window.location = “http://www.yourdomainname.com“;
}
else if(navigator.userAgent.indexOf(“MSIE”) != -1)
{
window.location = “http://www.yourdomainname.com“;
}
else if(navigator.userAgent.indexOf(“Netscape”) != -1)
{
window.location = “http://www.yourdomainname.com“;
}
else
{
window.location = “http://www.yourdomainname.com“;
}
</script>