There’s quite a bit of confusion over 301 redirects and how they effect SEO and a sites rankings.
I’ve extensively used permanent 301 redirects on both my own and SEO client sites and in this SEO tutorial I’ll explain how to use 301 redirects for a Linux Apache server and what you should expect SEO wise.
I’ve NOT included a redirect tutorial for Windows Server 301 Redirects since they don’t use a .htaccess file like you use with Apache servers. Also note adding 301 redirects using a .htaccess file is not the only way to add 301 redirects for a website running under Linux, there’s PHP redirects and server side redirects added via Apache config files, so this is just one of a number of ways to solve the SEO problem of www and non-www canonical URLs.
First some basic information for those new to permanent redirects.
What is a permanent 301 Redirect
A 301 or permanent redirect basically tells a browser (or search engine spider) that the site or page it is accessing has moved permanently. It then immediately redirects that page to the new one.
Search engines like Google will attempt to pass SEO benefit (PR and anchor text benefits) from the old page to the new page. Note: there is a small SEO cost to a 301 redirect, it’s a dampening factor added by Google to prevent the black hat SEO misuse of 301’s. The SEO cost is estimated to be around 15% of the PR/link benefit passed through a 301 redirect is lost. This is similar to the cost a standard link, when PR passes through a link to another web page it ‘consumes’ ~15% of the PR/link benefit.
How to Setup a 301 Redirect Within your .htaccess File
There’s several ways to setup a 301 redirect, probably the easiest is via your .htaccess file since it doesn’t require access to the server, just FTP access. The .htaccess file is a text file that you add to your site to store site and even directory specific server information (you can have a .htaccess file for every directory or just one for the whole site). Basically it adds to or overrides the current server settings.
If you have trouble creating a .htaccess file (some text editors won’t create a file named .htaccess) download this example one below (or one of the others later) and edit to your needs.
Example .htaccess file (all the code mentioned in this article)
A Simple 301 Redirect
A simple 301 redirect that redirects an old page to a new page for example to redirect stallion-theme.co.uk/old-seo.htm to stallion-theme.co.uk/new-seo.php do the following.
Open your .htaccess file in a text editor (like Notepad) and add the following code:
RewriteEngine On Redirect 301 /old-seo.htm https://stallion-theme.co.uk/new-seo.php
Or download this .htaccess file and edit to your needs.
The first part (RewriteEngine On) might not be needed, but just in case it’s turned off on your server add it (only needs to be added once per .htaccess file).
Save the file and upload the .htaccess file to the root of the domain that hosts the old page: “root of domain” means if you wanted to load it with a browser it would be found at https://stallion-theme.co.uk/.htaccess (this will throw out a forbidden error if you try to load it).
Now when accessing stallion-theme.co.uk/old-seo.htm it will immediately redirect to stallion-theme.co.uk/new-seo.php and Google etc… should pass any link benefit (PR and anchor text benefits) to the new page.
Be patient since it takes time for Google etc… to re-index the page and pass benefit to the new page, during this period (usually within 6 weeks) you may see a drop in SERPs, but it should be temporary.
You can use the same procedure above to redirect to another site, if we added the following to a .htaccess file on seo-gold.com
RewriteEngine On Redirect 301 /old-seo.htm http://www.stallion-theme.co.uk/new-seo.php
or the code below to use the same file name as the old site-
RewriteEngine On Redirect 301 /old-seo.htm http://www.stallion-theme.co.uk/old-seo.htm
When we load seo-gold.com/old-seo.htm it will redirect to stallion-theme.co.uk/new-seo.php or stallion-theme.co.uk/old-seo.htm respectively.
Download this .htaccess file and edit to your needs.
Now you know how to setup a simple 301 redirect one page at a time for varying effects.
301 Redirects and www/non-www Canonical Issues
There’s two ways to view most sites, that’s the www and non-www version. I won’t go into the full history of www, it’s enough to know when the first websites went online they were put in the folder www on the server (World Wide Web) and to access them you had to use the URL structure www.domain.com and it basically became the standard way to access a site via a browser. But, it’s not the only way, the true ‘location’ of the files are under domain.com: the www is in fact a sub-domain, you can have a completely separate site at www.
This has led to problems with search engines like Google indexing both versions resulting in duplicate content and sharing link benefit problems. Some webmasters link to the www version and others the non-www, search engine spiders follow the links and spider the site twice.
The major search engines have the ability to determine the www and non-www versions are the same and combine the results (passing all the benefit to just one version), but occasionally it fails and we see both versions indexed, we call this a www/non-www canonical issue. This is bad for the site owner since link benefit (PR/anchor text) is shared over two sites (making it harder to gain competitive SERPs).
You should also read about the canonical link element which is a sort of meta tag added to the head of a web page to inform Google etc… what the preferred (canononical) URL is. Canonical link element support is built into WordPress for example.
To check if you have a www/non-www canonical issue perform a site: search in Google-
site:http://domain.tld
The above search will find all pages indexed under the domain including the www and non-www versions. If you have a small site look through what is indexed, if you find both www and non-www pages indexed you have a canonical problem.
For larger sites the next search will only find the www version-
site:http://www.domain.tld
If there’s differences you might have a canonical problem. To be safe use the .htaccess code listed below as then you’ll never have to worry about www and non-www canonical problems again.
301 Redirects Fixes the www/non-www Canonical Issue
Fortunately a small amount of code within your .htaccess file located at root can solve the canonical problem. What this code does is 301 redirect one form of the site to the other, it’s like you setup an unlimited number of the simple 301 redirects I listed earlier on the fly.
If you use the www version use this code
RewriteEngine On RewriteCond %{HTTP_HOST} !^www.stallion-theme.co.uk$ RewriteRule (.*) http://www.stallion-theme.co.uk/$1 [R=301,L]
The code above will 301 redirect the non-www version to the www version. Obviously replace stallion-theme and co.uk with your domain name/tld.
Download the 301 redirect code in a pre-made .htaccess file and edit to your needs.
If you use the non-www version use this code
RewriteEngine On RewriteCond %{HTTP_HOST} !^stallion-theme.co.uk$ RewriteRule (.*) https://stallion-theme.co.uk/$1 [R=301,L]
The code above will 301 redirect the www version to the non-www version. Obviously replace stallion-theme and co.uk with your domain name/tld.
Download the 301 redirect code in a pre-made .htaccess file and edit to your needs.
More Complex Permanent 301 Redirects
The above 301 permanent redirects cover most situations you’ll come across, below I’ll post a couple of detailed permanent redirects for when you want to change domain names for an existing site whilst avoiding any www and non-www canonical problems.
www version
RewriteEngine On RewriteCond %{HTTP_HOST} !^www.seo-gold.com$ RewriteRule (.*) http://www.stallion-theme.co.uk/$1 [R=301,L] RewriteCond %{HTTP_HOST} !^seo-gold.com$ RewriteRule (.*) http://www.stallion-theme.co.uk/$1 [R=301,L]
The code above will 301 redirect both the www and non-www version of seo-gold.com (the old domain) to the WWW version of stallion-theme.co.uk (the new domain). Add this .htaccess file to the OLD site and upload the files from the old site to the new to see a seamless switch from an old domain to a new one.
Non-www version
RewriteEngine On RewriteCond %{HTTP_HOST} !^seo-gold.com$ RewriteRule (.*) https://stallion-theme.co.uk/$1 [R=301,L] RewriteCond %{HTTP_HOST} !^www.seo-gold.com$ RewriteRule (.*) https://stallion-theme.co.uk/$1 [R=301,L]
The code above will 301 redirect both the www and non-www version of seo-gold.com (the old domain) to the non-www version of stallion-theme.co.uk (the new domain). Add this .htaccess file to the OLD site and upload the files from the old site to the new to see a seamless switch from an old domain to a new one.
Expect some temporary (under 6 weeks) SERPs drops as Google etc… take the new 301 redirects into account.
Download both versions of the code above in a pre-made .htaccess file and edit to your needs.
Don’t forget when moving domains you should also add a .htaccess file that deals with canonical problems to the new domain (the code under the heading “301 Redirects Fixes the www/non-www Canonical Issue”).
This article should cover the vast majority of scenarios requiring 301 redirects.
David Law
How to Hide wp-content/plugins/ folder
Hi David,
how do you hide your plugins?
Thanks
Héctor
How to Hide Website Directories
Hiding a directory is a server side setting.
Assuming you run on an Apache server it’s having something like:
Into your httpd.conf file for each domain on a server, this would make no directories that lack an index.html, index.php etc… show what’s in them.
If you know how to edit the httpd.conf file you’ll be looking for instances of something like this to change:
Another way is create an index.php file and add this to it:
Upload this to any folder you want ‘hidden’, first check there isn’t a index.php file in the directory.
Users now get a blank webpage where ever you upload index.php to. I use this file in Stallion Responsive, load any of the Stallion Responsive folders in a browser like : https://stallion-theme.co.uk/wp-content/themes/stallion-responsive/widgets/
And it’s a white page, simple and effective, stops your visitors snooping inside your themes files and stops Google inadvertently easily spidering and indexing anything in the Stallion Responsive directories that shouldn’t be indexed. I don’t understand why WordPress by default doesn’t do this, doesn’t make any sense to allow users and search engines to see what’s inside /wp-includes/!
I’m not sure if every time you auto update WordPress /wp-admin/ and /wp-includes/ are deleted, if so if you want these hidden you’d have to upload the .htaccess file or the index.php file again after each update.
Best solution is the server side one, but can be beyond a lot of webmasters to edit the httpd.conf file. You might have a setting under your control panel, I use the control panel Virtualmin and I couldn’t find an option so added the code manually which on my servers is under “/etc/httpd/conf/httpd.conf”.
David
How to Hide Website Directories
WordPress .htaccess File
Hi David,
I use bluehost, it shows this way on my FTP session
I tried adding this .htaccess inside of plugins, but it didn’t work
I wonder if it’s because already exists a index.php within the directory?
Regards,
Héctor
WordPress .htaccess File
301 Redirect Everything Under a Directory via .htaccess
Hmm, after rereading what I suggested the .htaccess option isn’t suitable for all directories like /wp-content/plugins/ it would 301 redirect everything under the folder to home rather than just stop users seeing the contents!
I’ve deleted that part of my earlier comment and pasted below with a better modification to explain what it actually does.
How to 301 redirect an entire directory to home.
You have two directories you no longer use and want to 301 redirect everything that used to be in them to home, this could be thousands of images or webpages.
/wp-content/hidden/
and
/oldstuff/
Add this to your root .htaccess file
Note if “RewriteEngine On” is already within the file it’s not needed again.
This would 301 redirect everything under the folders “/wp-content/hidden/” and “/oldstuff/” to my home page.
This can be useful where you’ve deleted hundreds of tags, tags are rubbish SEO wise and should be avoided, if you deleted all your sites tags you could build a set of 301 redirects to conserve the link benefit.
You’ve delete all tags and want them all 301 redirecting:
You’ve deleted some tags called tag 1, tag 2 and tag 3, but kept tag 5 and tag 5
Tag 1 to 3 will 301, the rest won’t be.
Best to try to find a page that’s similar to redirect to rather than just back to home
Above I don’t have a relevant page to tag-1 so 301 to home, but do to tag-2 have a post about tag 2 and have a category similar to tag 3.
I have sites with dozens of 301 redirect rule sets like these.
David
301 Redirect Everything Under a Directory via .htaccess