Redirects are just one small slice of the SEO pie, but they can be a fairly important slice. It’s one small change that could do a lot for the user’s experience and your ranking.
One note up front: this article will only cover 301 redirects (that, is “permanently moved”) because they’re the most commonly used. Additionally, it will only make use of .htaccess on an Apache server and a few alternative methods. It will not cover setting up redirects on any version of IIS servers since they’re far less popular and a little too complicated for an article of this scope. If you’re not sure what kind of server your site is on, plug the URL into builtwith.com. All your server information should be at the top of the results page. If it says Apache, you’re good to go, so read on!
What Is a Redirect?
Without getting technical, a redirect is kind of like a permanent detour sign. To give a literal example, let’s say your original page was at example.com/2013-coupons. If you set up a redirect from example.com/2013-coupons to example.com/2014-coupons and then you type /2013-coupons into your browser, it will send you to /2014-coupons instead.
Pretty simple, right?
An .htaccess (short for hypertext access) file is used to configure or enable certain features on a supported web server. One of those features is redirects.
To get your .htaccess file, log into your site via FTP. You should find the .htaccess file in the root directory for your site. If it’s not there (it’s not a required file, so it may not exist on your site yet), create a blank file in Notepad and save it. Once it’s saved, rename it “.htaccess”; no quotes, no spaces, no .txt extension. Once renamed, upload it to the root directory of your site. When you want to make changes, just open this file in Notepad or an equivalent program. When you’re done, save it and upload it, overwriting the previous file.
Using .htaccess can be a little dangerous to your site if you make a mistake. Here are some safety tips:
- ALWAYS save a backup before making changes, just in case
- Notate your manual entries so you know what they do; this is done by adding a # at the beginning of each line (known as commenting out a line)
- Don’t remove anything already in your .htaccess; it’s probably there for a reason
When, Why, and How Should I Use a Redirect?
As far as SEO is concerned, there are several instances where setting up redirects can be useful:
When You Change a URL
Whether you just tweak a single page’s URL or you change the entire domain, you should set up redirects from the old URL to the new one.
When you change a URL, all the external links to your old site will break. Then, when a user clicks one of those links, he or she will be met with an ugly 404 page instead of the wonderful content they wanted to see on your site. Additionally, Google will think the new URL is a brand new page and its ranking will reset.
Using a redirect in this situation ensures all those links will continue to work and when Google indexes your site again, it will realize your page was just moved and transfer all the metrics you’ve built up on the old page.
This snippet will apply a 301 redirect to specific pages:
The instructions are already commented in.
This code will redirect an entire site:
Again, the instructions are commented in.
And it’s as easy as that!
To Consolidate www and Non-www
Many sites you visit will redirect you to either the www or non-www version of the URL. Take OrangeSoda’s site for example. If you type orangesoda.com into your browser, it will show up as www.orangesoda.com when it loads.
The reason for doing this is that when Google crawls your site, it will index the www and non-www versions as two different sites. This wouldn’t be so bad, but sometimes you’ll get flagged for duplicate content and your ranking will drop. It’s a little silly that Google interprets it this way, but the fix is really simple: just pick a version and redirect to it.
Note that this can also be resolved with canonical tags, but a redirect is preferable for user experience. A canonical tag basically tells Google, “Hey, this page and that page are actually the same page, not duplicates”. You can learn more about it in our blog post on canonical tags.
This snippet looks a lot more complicated than the last two, but you don’t have to change anything! Simply paste it in and it will redirect everything to www:
Remember, do not change anything.
If you can’t use .htaccess and you have access to your DNS settings, you can point your non-www A record (denoted by “@”) to 220.127.116.11. This sends all non-www requests to a free service that just redirects to the www version of your site. Once the change propagates (it usually takes 20-60 minutes, but sometimes longer), your site should be redirecting. I would only do it this way if you cannot use .htaccess because you’re leaning on a 3rd party to keep your site up while .htaccess is all local to your server.
To Consolidate Homepage URLs
Many sites’ homepage can be accessed through both the root domain (that is, the URL without anything after the /) and the specific homepage address (commonly /index.html, /index.php, or /default.asp, etc.). Using OrangeSoda’s page as an example again, go to www.orangesoda.com/index.php. It will redirect you back to the root domain.
You should consolidate these homepage URLs to your root domain for the same reason you should consolidate www and non-www: duplicate content flags. Again, Google will think each of your homepage URLs is a unique page with the exact same content and ding your ranking.
This snippet will redirect any page you want to the root domain, but you usually only want to do that with the examples given in the When/Why section. Here it is:
Note that /index.html appears twice in that snippet. If you needed to redirect /index.php, you would need to replace both instances of html with php. For /default.asp, you would need to replace both instances of index and html with default and asp, respectively. Remember to plug your domain in for example.com too!
And that’s everything you need to know about redirects! If you’re interested in learning more, here are some additional resources: