At some point all of us needed to add a redirect of some type to our website. Whether it will be for SEO purposes or to point ur visitors to the new website version, various types of redirects were always needed. All of them will report 301 or 302 HTTP status code to your browser, so no worries about ruining your SEO strategy :)
Below I will describe the most common ones, which can be safely used on a standard shared hosting:
.htaccess redirect
The ,htaccess file (where applicable) is a part of the Apache web server and is always parsed when present in a directory. All of the commands and conditions in it apply recursively and are always parsed first. It has higher priority than all other files in the same directory and redirect rules in it will overwrite others. It is always parsed from the top side down, so whatever rule you place at the top will always take place first, which may negate others placed bellow. To redirect your visitors to a new location using .htaccess we have several options:
to redirect our visitors from your-domain.com to www.your-domain.com:
RewriteEngine on
rewritecond %{http_host} ^your-domain.com [nc]
rewriterule ^(.*)$ http://www.your-domain.com/$1 [r=301,nc]
Lets break this down into pieces, so you can understand how it works:
RewriteEngine on
This tells the Apache web server, that we will use mod_rewrite (this is the Apache module, which actually performs the redirects) and tht it should be enabled for this directory. Usually md_rewrite is enabled by default, but some hosts leave it disabled in your vhost configiration, so to be on the safe side, we place this line at the top.
rewritecond %{http_host} ^your-domain.com [nc]
This is the rewrite condition. If this condition is not met, no redirect will take place. This one tells Apache, that if we are accessing this resource via your-domain.com, then the following rule should take place:
rewriterule ^(.*)$ http://www.your-domain.com/$1 [r=301,nc]
This is the actual redirect rule. It tells apache to forward the visitor, which typed in your-domain.com to the new address:
http://www.your-domain.com
The variable $1 stands for the second part of the URL, which your visitor typed in in his browser after your-domain.com. For example, if you have this set of rules in your .htaccess and I type in:
http://your-domain.com/contact.html
I will be forwarded to:
http://www.your-domain.com/contact.html
The same rule can be used to forward visitors of you have changed your domain name to a new one.
You can also use .htaccess to redirect visitors from one page to another. Lets say, that you have a page named contact.html (where your site’s cntact form is located), but you want your visitors to use the new enhanced contact from now on. To make sure noone accesses the old one, you wimply add a redirect rule, which forwards visitrs from the old one to the new one. The rewrite set would look like this:
RewriteEngine on
Redirect 301 /contactus.html http://www.your-domain/new-contactus.html
In this particular case, the .htaccess file must reside in the same folder as the contactus.html file.
If you have a webstore or just have an SSL certificate and would like to force your visitors to use it, you can also employ a .htaccess based redirect rule to make this happen. Here two different sets of rules can be used:
RewriteEngine On
RewriteCond %{SERVER_PORT} !443
RewriteRule (.*) https://www.your-domain.com/ [R]
This will tell the web server, that if the visitor is not accessing this resource on port 443 (which is the port on which SSL work), the visitor should be automatically forwarded to a new URL. There are some servers, which do not use the standard SSL port and have their own or a dynamic one. In such cases, a better, but slightly more compliacated redirect should be applied:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
HTML redirect
If your web host is not using Apache r you just don’t want to edit your .htaccess file, you can easily add an html file, which will do the job. Although, html is far from sophisticated, it can easily handle a simply redirect. If we take the above example and would like to forward our visitors from our old domain to our new one, simply add a files named index.html with the following code in it:
<meta http-equiv=”refresh” content=”10; url=http://your-new-domain.com”>
The content variable in this case is for how long should the browser wait until it forwards you to the new domain name. The values are in seconds. In this particular case, I have set 10, so another message can be also displaye don the page for my visitors to read.
The url variable is for the location of the new page/domain.
PHP redirect
The third option to use a redirect is via another language, which almost every host has nowerdays — PHP. Just as with HTML, it is only capable of carrying out a simple redirect from one location to another. To use it, simply create a file named index.php in the directory, from which you would like to redrect your visitors and add the following lines at the top of the file:
<?php
header( ‘Location: http://www.your-new-domain.com’ ) ;
?>