To keep the lights on, we receive affiliate commissions via some of our links. Our review process.
As part of our series on domain names, we’re going to discuss website 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
- Redirect To WWW Or To Non-WWW?
- Rewrite Engine
- Redirecting TO WWW (www.example.com)
- Redirecting TO NON-WWW (example.com)
- Specifying WWW Redirects In Webmaster Tools
- Redirecting Pages Indexed By Search Engines
- Redirect From One Domain to Another
- Redirect From Old Domain To New Domain (Keeping Files And Folders Intact)
- Redirect From Subfolder To Root
- Redirect From Subdomain To Subdomain
- Redirect From Folder To Subdomain
- Redirect From Non-WWW To WWW Or Vice Versa
- Redirecting From One File Extension To Another
- Redirect A File, All Extensions
- Keeping File Paths Intact
- Redirect All Files Within A Folder To Another Domain
- Redirects For Different Server Systems
- Redirecting Search Engine Indexed Pages
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 Search Console, 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.
Did you get your redirects working? Let us know in the comments!
Tagged With: