Introduction
We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways can do the website redirection.
Redirect using "Meta Refresh Tag" method
Create a simple html file with the below code. This will redirect the URL to
http://newwebpage.com
HTML Syntax: <META http-equiv=”refresh” content=”<time>[;URL=<new url>"]>
<head>
...head section stuff (Title, Description,etc.)...
<meta http-equiv="refresh" content="0;url=
http://newwebpage.com">
</head>
Meta-tag method is on of easiest way to achieve the redirect the web pages. But it has some disadvantages. Crawlers used in search engines will not keep the old rank in the new web page. Disadvantage in "Meta Refresh Tag" method can be solved using 301 redirect.
What is 301 redirect.
301 redirect, interpreted as “moved permanently”, is a method used to redirect webpages while preserving your search engine rankings. There are different ways to achieve this.
Please note my examples are based on Apache web server.
Redirect with .htaccess and Mod_Rewrite
The easiest way to achieve redirection is using .htaccess, since it doesn’t require access to server. We can make use of FTP access and upload the .htaccess file.
Scenario 1.
Simple 301 redirect accessing a page to a newwebpage.
RewriteEngine On
Redirect 301 /mypage.htm
http://newwebpage.com
Save the file and upload the .htaccess file to the root of the domain that hosts the old page. This will do a permanent redirect the page to the newwebpage.com and get the advantage of old web site ranking as well.
Scenario 2
www and non-www are not same for search engines
There are two ways to view most sites, that’s the www and non-www version www.domain.com and it basically became the standard way to access a site via a browser. However it’s not the only way, some sites can be browsed using http://domain.com which in the non-www version.
This has led to problems with search engines like Google indexing both versions resulting in duplicate content and sharing link benefit problems. Some webmasters link to the www version and others the non-www, search engine spiders follow the links and spider the site twice.
.htaccess file to redirect non-www site to www
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteRule ^/(.*)
http://www.domain.com:%{SERVER_PORT}/$1 [L,R]
Scenario 3
Redirect Homepage of a site according to the ``User-Agent:'' header of the request
You might have noticed some websites will load different pages when browsed from different browsers. The simple logic is to have multiple websites and have the below in .htaccess file.
RewriteCond %{HTTP_USER_AGENT} ^Mozilla
RewriteRule ^/$ /homepage.max.html [L]
RewriteCond %{HTTP_USER_AGENT} ^Lynx
RewriteRule ^/$ /homepage.min.html [L]
RewriteRule ^/$ /homepage.std.html [L]
Scenario 4
Protecting your images and files from linking
If you do not want other webmasters linking to your website files and images as inline-images on their pages use the below in .htaccess file.
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$ [NC]
RewriteCond %{HTTP_REFERER} !^http://domain.com [NC]
RewriteCond %{HTTP_REFERER} !^http://www.domain.com [NC]
RewriteCond %{HTTP_REFERER} !^
http://201.192.1.168 [NC]
RewriteRule ^.*$ http://www.domain.com/ [R,L]
Comments (1)
Commented:
e.g. in php...
<?php
header('Location: http://www.example.com/');
?>