Search Friendly Domain and File Redirects

To sustain this free service, we receive affiliate commissions via some of our links. This doesn’t affect rankings. Our review process.

www on laptop screenAs 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, redirect the one with lower PageRank to the one with higher PageRank.

If you’re using Apache/ .htaccess you can use the examples below. For other systems, scroll down.

Rewrite Engine

For all these examples, make sure you turn on your rewrite engine with the following line.:

RewriteEngine on

We have added it, for your convenience, to all the relevant examples so you can copy and paste all the lines at once.

Redirecting TO WWW (www.example.com)


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

Redirecting TO NON-WWW (example.com)

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

Specifying WWW Redirects In Webmaster Tools

The top three search engines offer a version of webmaster tools that allow you to see how your website and its pages are being indexed by the respective search engine. In the Google webmaster tools, for example, you can specify which URL version (example.com or www.example.com) you are using, and Google will make the adjustments to ensure credit is being directed only to that version. We highly recommend that, in addition to this, you redirect your pages to your preferred version using the sample code snippets above in your .htaccess file.

Redirecting Pages Indexed by Search Engines

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.

Redirect from One Domain to Another

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?kanine\.com
RewriteRule ^(.*)$ //www.canine.com/$1 [R=301,L]

This approach is most useful if you have aliases of a specific domain name. For example, you may have Kanine.com, a common misspelling, redirect to Canine.com, as illustrated in the code snippet above.

Specifying Files Not to be Redirected

Let’s say you want to redirect every file and folder on a domain name, with the exception of one or more. How do you specifically tell the website that’s being redirected from not to redirect certain files? Here’s how – simply add this line to your .htaccess statement above:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?kanine\.com
RewriteCond %{REQUEST_URI} !^/not-this-page/?$
RewriteRule ^(.*)$ //www.canine.com/$1 [R=301,L]

This will redirect all pages and files from kanine.com to canine.com, with the exception of kanine.com/not-this-page (and kanine.com/not-this-page/). Add as many of these lines as you like. You can also specify directories with wildcards to exclude entire directories.

One situation where this is particularly useful is when you want to tell Google Webmaster Tools that you’re changing your website address, as discussed above. You’ll need to verify your old domain name, and to do this, you’ll need to exclude the Google verification file from being redirected.

Redirect from Old Domain to New Domain (Keeping Files and Folders Intact)

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) //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 Subfolder to Root

RewriteEngine on
RewriteRule ^subfolder/(.*)$ /$1 [R=301,NC,L]

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 Folder to Subdomain

Because this can become exceedingly complex, especially if you’re trying to redirect a subdomain folder to a subdomain, we’ve written up a separate article on this. Read our redirect folder to subdomain article for details.

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:

Redirecting from one file extension to another

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$ //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$ //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$ //www.example.com$1.php
RewriteRule ^index\.php$ //www.example.com/ [R=301,L]

Redirect a File, All Extensions

To redirect a file, regardless of extension, simply use the following (Apache):

RewriteRule ^filename(.*)$ //www.example.com/filename$1 [R=301,L]

If you want to specify a specific filename with extension, use the following:

RewriteRule ^filename.htm$ //www.example.com/filename.htm$1 [R=301,L]

Apache

Simply add the follow directive to your .htaccess file:

RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*)$ //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 (these are alternative methods to those described earlier in this article. If you’re having trouble with one, try the other):

www to non-www

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

non-www to www

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

Redirect All Files Within a Folder to Another Domain

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/(.*)$ //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 //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.

Redirects for Different Server Systems

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.

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, //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: //www.example.com" );
?>

ASP

<%@ Language=VBScript >
<% Response.Status="301 Moved Permanently" Response.AddHeader "Location", " //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","//www.example.com");
}
</script>

ColdFusion

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

Redirecting Search Engine Indexed Pages

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.

Alex bring a series of in-depth articles on search marketing and content management systems as well as troubleshooting tips to We Rock Your Web's collection. He is an avid tennis player, nature enthusiast, and hiker, and enjoys spending time with his wife, friends, and dogs, Bella and Lily.

18
Leave a Reply

avatar
newest oldest most voted
a web rocker
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.
Anonymous
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!

a web rocker
I am a newbee to Coldfusion. How do you hide the blog id create with CreateUID() function?

a web rocker
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.

a web rocker
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.

a web rocker
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: 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.

a web rocker
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.

a web rocker
I am hosting my site 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 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.

a web rocker
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.

a web rocker
It never ceases to amaze me how much easier it is to make redirects work in Apache compared to Windows or other servers. Apache and .htaccess make my life so much easier! Not going back to an IIS/ Windows server anytime soon…