As part of our series on domain names, we're going to discuss web site redirects, and why and how they should be implemented. Traditionally redirects are used to guide a user to the new location of an outdated page. However, they are also important on the domain level. To be precise, one of the first decisions you should make for your domain URL is whether it will be appended by a "www" For example, www.example.com vs example.com. Why do you need to pick one over the other?
Long story short, because users tend to type one or the other in the browser bar, and search engines may in fact treat both versions differently. This can result in your site's PageRank, as well as link popularity, being divided in two! The solution? The answer is to create a 301 (permanent) redirect from one to the other.
It's important for SEO purposes that you use a 301 redirect (permanent redirect), and not 302 (temporary). Most registrars use a term called domain forwarding, or URL forwarding. In most cases this will simply create a 302 redirect; few registrars to our knowledge apply 301s. The solution is for you to host the domain yourself and input the code below into your .htaccess file.
Which one to redirect to? If you are just starting out and neither version has established a PageRank, we suggest you go with the "www" version. If, however, one version has already established a PageRank over the other (Visit our PageRank Calculator to find out what your site's PageRank is), redirect the one with lower PageRank to the one with higher PageRank.
One of the most common (and important) reasons for redirecting your website pages is to maintain your search engine rankings when you transfer to a new hosting server or domain name. Skip straight to the bottom of this article, and the section redirecting search engine indexed pages, to learn more.
Make sure you turn on your rewrite engine with the following line:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?contract-web-development\.com
RewriteRule ^(.*)$ http://www.contractwebdevelopment.com/$1 [R=301,L]
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.new-domain.com/$1 [R=301,L]
In this example replace new-domain.com with your new domain name. Thanks to the +FollowSymLinks line, all folders and files will redirect to your new domain name - ie. the only thing that will change is the domain name.
If you'd like to redirect from one subdomain on a website to another, use the same redirect code listed above under redirect from one domain to another, but this time, place the redirect code in directory of the subdomain. That is, if you want to redirect sub.example1.com to sub.example2.com, you'll need to place the redirect (ie. .htaccess file) in the root folder of subdomain sub.example1.com.
There are several ways to implement a 301 redirect. The codes below for Apache and IIS redirect to www (from non-www) and will redirect corresponding subdirectories as well. Pick the one according to your needs and substitute in your domain name:
Let's say you've added scripting to your website, and all your filename extensions are changing from .htm to .php. It's vital that you let search engines know that you're changing your file extensions, so they index the correct files and you don't get penalized for duplicate content.
In this example, we redirect all .htm files to their .php counterparts:
RewriteEngine On
RedirectMatch 301 (.*)\.htm$ http://www.example.com$1.php
Note that this will also redirect your index file. What this means is, if your index file was index.htm, your browser will now redirect to index.php when you load your site. This is not desirable, as your browser, and the search engines indexing your site, should load your root page (ie. example.com/), and not your index file directly (ie. example.com/index.php). Use the following code to set your directory index and redirect your index file back to its root:
Options +FollowSymLinks
DirectoryIndex index.php
RewriteEngine On
RewriteRule ^index\.php$ http://www.example.com/ [R=301,L]
Note that if you combine the above two code snippets, which you probably should, you will only need the Rewrite statement once. Below is all the code combined into one snippet:
Options +FollowSymLinks
DirectoryIndex index.php
RewriteEngine On
RedirectMatch 301 (.*)\.htm$ http://www.example.com$1.php
RewriteRule ^index\.php$ http://www.example.com/ [R=301,L]
To redirect a file, regardless of extension, simply use the following (Apache):
RewriteRule ^filename(.*)$ http://www.example.com/filename$1 [R=301,L]
If you want to specify a specific filename with extension, use the following:
RewriteRule ^filename.htm$ http://www.example.com/filename.htm$1 [R=301,L]
Simply add the follow directive to your .htaccess file:
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
You can modify the above to redirect www to non-www or vice versa and keep file paths intact:
rewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L] )
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
Let's say a directory on your site has grown so big that you've decided to create a whole new website dedicated to its content. You may want to redirect files as is, in which case you'd use the example above for keeping file paths intact. If, however, you're starting over from scratch and simply want all the old links redirected to the root page of your new website, you could use the following:
RewriteRule ^directory/(.*)$ http://www.example.com/ [R=301,L]
All files in example.com/directory, including the directory itself, will be redirected to example.com. Another way to do this is to use RedirectMatch (from mod_alias instead of mod_rewrite):
RedirectMatch ^/directory http://www.example.com/
The potential issue with this approach is if your site is using frames. In this case, the new site may load within a frame of the old directory. In which case you'd be better served using the rewrite approach above.
So far all the examples have assumed you're using an Apache server environment. Below are some examples for Windows and other Linux servers and various coding environments.
Directly edit your local httpd.ini file:
RewriteCond Host: ^example\.com
RewriteRule (.*) http\://www\.example\.com$1 [I,RP]
<?php
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://www.example.com" );
?>
<%@ Language=VBScript >
<% Response.Status="301 Moved Permanently" Response.AddHeader "Location", " http://www.example.com" >
<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location","http://www.example.com");
}
</script>
<.cfheader statuscode="301" statustext="Moved permanently">
<.cfheader name="Location" value="http://www.example.com">
One of the most important reasons for using the above redirects is to redirect your pages that are indexed by search engines when you transfer your website to a new server or domain name. To find out how to do so, read our article on how to transfer your website and maintain your search ranking.
All Content © 2007 - 2009 Contract Web Development, Inc. All Rights Reserved. Privacy Policy | Terms of Use | Powered by Drupal
Eclipse won't recognize CF8 syntax
I can't get Eclipse to recognize CF8 syntax. I'm running Linux and Eclipse 3.2. I added the CF8 extensions from Macromedia using the ZIP as a source, all went well but It's not recognizing CF8 syntax. I also followed the CF8 Dictionary instructions, and found the cf8.xml file was already there so I assume the above install worked. In my CFEclipse Project/Properties I still only see language up to 7.01.
Is there a mistake with your non-www to www code?
I think the first line of code in the non-www to www code should be
rewriteEngine On
I have also seen some people add:
Options +FollowSymLinks
as the first line in the non-www to www code.
Is there a reason you did not?
Thanks
RewriteEngine On
You're correct - RewriteEngine On is definitely necessary. We mention it in the article in the beginning, and then don't repeat it for each code segment. In other words, you only need RewriteEngine On once, at the top of your .htaccess file. After that, the "Rewrite Engine" is on, unless you turn it off again with RewriteEngine Off.
Options +FollowSymLinks is only necessary if you want to redirect all your existing paths.
Redirecting indexed pages
Thanks, these are useful redirects. I'd like to point out that a good use of these is to redirect pages indexed in search engines. You might want to point this out to your readers.
Added article on redirecting of indexed pages
Thanks random drifter
We've added a section and article on redirecting indexed pages (see above), with details on how to search for indexed pages in the top three search engines (Google, Yahoo, MSN Live).
Can you use a domain redirect to compensate for bad DNS
We are having issues with competitors buying domain names and using a 3rd party DNS service and applying our IP address to their domains. This creates a duplicate content issue for us. Rather than wasting our time trying to wrangle with the DNS services to get this problem solved, we thought we might be able to redirect at the TLD level, but none of the scripts we've tested actually work - they all seem to be designed to function in legitimate cases where someone's hosting more than one domain on the same server and they want to redirect into a subdirectory on the same machine.
Is there a way to script (in PHP or Javascript) a redirect that looks at the HTTP_HOST value and just makes a wholesale change to the domain name? I've tried a couple that claimed to do this but I get a blank page.
Example, on my index.php file I've tried this:
<?php
$host=getenv(HTTP_HOST);
switch ($host) {
case 'baddomain.com':
header("Location: http://www.ourdomain.com/index4.php");
exit();
case 'www.baddomain.com':
header("Location: http://www.ourdomain.com/index4.php");
exit();
default:
header("Location: http://www.ourdomain.com/index4.php");
exit();
}
?>
I don't care if the baddomain.com gets penalized for the redirect, it's not my domain - i just don't want to have my website's content being displayed verbatim when someone else's domain is loaded up. If I can create somewhat useful page to redirect their domains to, maybe eventually they will stop this childishness.
Moving from ASP to PHP
I have a client who is concerned about their page rank with a move from .ASP to PHP. What can I do to make this transition less painful, and not lose rankings
Search-friendly ASP to PHP redirect
Changing any part of a domain, filename, or a filename extension creates what search engines consider to be a new page. You'll therefore need to use 301 (permanent) redirects to let the search engines know that the filename extensions have been updated to .PHP. Simply map all requests for "ASP" to "PHP"
Make sure you keep the redirects in place for at least 3 - 6 months. You will see a temporary reduction in Pagerank but it should be restored in the long term.
I'm new to Apache...
Would I do this using htaccess?
ASP to PHP Code Sample
Yes, copy and paste the following into your .htaccess file:
RewriteEngine On
RewriteRule ^(.*).html$ $1.php [R=301]
Redirect using Cold Fusion
I am hosting my site http://example.com using a shared server to which I don't have access to IIS. I would like to redirect all traffic from http://www.example.com to the www-less version of the URL. The http://example.com version of the URL ranks much higher than the www version. Is it worth my time doing the ColdFusion redirect on my index page or will it simply cause a slowdown on page load.
ColdFusion redirects
Since two page requests are being made redirects may cause a minor slow-down (as with any rewrite), but it shouldn't be worrisome and it's probably worthwhile giving your site one identity and consolidating PR. Just make sure the redirect is 301 (permanent), and not 302 (temporary).
distinguishing redirects
Please clarify the difference (Linux-Apache) between an "add on domain", a "domain redirection", and a "parked domain". Other than an "add on" going to a subdirectory on a main domain, the differences between the 3 have not been clearly explained by your pages, wiki, or any other site I have found. Just a 1 paragraph summary to make it clear would really help. Thanks.
Parking, redirect, addon domains
It just so happens that I wrote an article on that a few weeks ago:
What is the Difference Between Parking, Redirect, and Add-On Domains?
Let me know if it answers your questions - if not, I'll write more.

ASP.NET Redirect?
Do you happen to have the redirect for ASP.NET as well? I have been looking for one that works for some time now and can't quite get it right.
Thanks so much!
ASP.NET added
Hi Susan,
I've added the redirect for ASP.NET.