Jun 10 2008

Search Friendly Domain and File Redirects

Categories:

Domain Level Redirect

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.

Permanent (301) vs Temporary (302) redirects

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.

Redirect to WWW or to non-WWW?

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.

Rewrite Engine

Make sure you turn on your rewrite engine with the following line:

RewriteEngine on

Redirect From One Domain to Another

RewriteCond %{HTTP_HOST} ^(www\.)?contract-web-development\.com
RewriteRule ^(.*)$ http://www.contractwebdevelopment.com/$1 [R=301,L]

Redirect from old domain to new domain (keeping files and folders intact)

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.

Redirect from subdomain to subdomain

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.

Redirect From non-www to www or Vice Versa

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:

Apache

Simply add the follow directive to your .htaccess file:

RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

Keeping file paths intact

You can modify the above to redirect www to non-www or vice versa and keep file paths intact:

www to non-www

rewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L] )

non-www to www

RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

IIS 5/6

  • For the domain you're moving from, go to IIS Site Properties. Then select "A redirection to a URL." on the home directory (since we are redirecting the root folder, or domain)
  • Next, enter the domain you wish to move to in the redirect box, followed by "$S$Q," making sure not to add a trailing slash - for example, http://www.example.com$S$Q
  • Finally, select the option to sent the user to "The exact URL entered above", and "A permanent redirection for this resource."" (since we want a 301, not a 302 (temporary), redirect). 

ISAPI/ Rewrite

Directly edit your local httpd.ini file:

RewriteCond Host: ^example\.com
RewriteRule (.*) http\://www\.example\.com$1 [I,RP]

PHP

<?php
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://www.example.com" );
?>

ASP

<%@ Language=VBScript >
<% Response.Status="301 Moved Permanently" Response.AddHeader "Location", " http://www.example.com" >

ASP.NET

<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>

ColdFusion

<.cfheader statuscode="301" statustext="Moved permanently">
<.cfheader name="Location" value="http://www.example.com">

Average: 5 (1 vote)
Select your preferred way to display the comments and click "Save settings" to activate your changes.

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.
Smiling

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. Smiling

  • Allowed HTML tags: <a> <em> <strong> <cite> <ul> <ol> <li> <dl> <dt> <dd><u>
  • Lines and paragraphs break automatically.
  • Textual smileys will be replaced with graphical ones.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.