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, vs 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 (

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

Redirecting TO NON-WWW (

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC] RewriteRule ^(.*) //$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 ( or 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 ^(.*)$ //$1 [R=301,L]

This approach is most useful if you have aliases of a specific domain name. For example, you may have, a common misspelling, redirect to, 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 ^(.*)$ //$1 [R=301,L]

This will redirect all pages and files from to, with the exception of (and 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 (.*) //$1 [R=301,L]

In this example replace 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 to, you’ll need to place the redirect (ie. .htaccess file) in the root folder of subdomain

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$ //$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., and not your index file directly (ie. 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$ // [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$ //$1.php
RewriteRule ^index\.php$ // [R=301,L]

Redirect a File, All Extensions

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

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

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

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


Simply add the follow directive to your .htaccess file:

RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*)$ //$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 ^(.*)$ //$1 [R=301,L] )

non-www to www

RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC] RewriteRule ^(.*)$ //$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/(.*)$ // [R=301,L]

All files in, including the directory itself, will be redirected to Another way to do this is to use RedirectMatch (from mod_alias instead of mod_rewrite):

RedirectMatch ^/directory //

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, //$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]


Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: //" );


<%@ Language=VBScript >
<% Response.Status="301 Moved Permanently" Response.AddHeader "Location", " //" >


<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
Response.Status = "301 Moved Permanently";


<.cfheader statuscode="301" statustext="Moved permanently">
<.cfheader name="Location" value="">

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.

About The Author:

Alex has been involved on the business side of the internet since the early 2000's. He holds both a Management Science degree from the University of California at San Diego as well as a Computer Science degree from NJIT.

We Rock Your Web had its roots back in 2004 as the tech blog for a web design and development company Alex founded that has grown and evolved into the parent company of We Rock Your Web.

While his foundation is rooted in web development, his expertise today lies in content and digital marketing, SEO, organic and paid search, analytics, and publishing. Alex is an avid tennis player, nature enthusiast, and hiker, and enjoys spending time with his wife, friends, and dogs.

Disclaimer: This website contains reviews, opinions and information regarding products and services manufactured or provided by third parties. We are not responsible in any way for such products and services, and nothing contained here should be construed as a guarantee of the functionality, utility, safety or reliability of any product or services reviewed or discussed. Please follow the directions provided by the manufacturer or service provider when using any product or service reviewed or discussed on this website.

Notify of
Oldest Most voted
Inline Feedbacks
View all comments

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…

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

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.

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.

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.

Thanks! We’ve added a section and article on redirecting indexed pages, with details on how to search for indexed pages in the top three search engines (Google, Yahoo, MSN Live).

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

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.

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?

Yes, here’s an ASP to PHP code sample: Simply copy and paste the following into your .htaccess file (make sure you back it up first in case you mistype something!):

RewriteEngine On
RewriteRule ^(.*).html$ $1.php [R=301]

I am hosting my site using a shared server to which I don’t have access to IIS. I would like to redirect all traffic from to the www-less version of the URL. The 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.

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

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.

It just so happens that we wrote an article on that a few weeks ago: What is the Difference Between Parking, Redirect, and Add-On Domains? Let us know if it answers your questions.

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!

Hi there, we’ve added the redirect for ASP.NET 🙂