301 Redirect using .htaccess file – Apache 301 Redirect

written by William Patton on December 15, 2012 in Web Development with no comments

Creating a 301 redirect with a .htaccess file.

Redirecting one url to another has many uses and there are a few different ways to do it including php 301 redirects and htaccess 301 redirects. My prefered method and use, the Google aproved and standardised method, is to use a 301 redirect to tell the server that when a user visits a certain page in their browser the request to the server should be replied to with the new page instead of the page at the old address – which is probably a broken link. Creating a htaccess 301 redirect couldn’t be simpler. Simply put redirect 301 then the url that will be visited followed by the url to redirect the visit to.

What is a 301 redirect?

An Apache 301 redirect is a permanent redirect and there are many places where using a 301 redirect would be useful. One use for them is creating vanity urlsbut I most commonly use them for their intended purpose of moving a page from one address to another.

Vanity Urls

A vanity url can be used to improve the user experience on your site or within the context campaign tagging for your site analytics. It improves the UX by making urls easier to type. www.domain.com/buythis is much easier to type and remember than www.domain.com/store/products/product-123?utm_campaign=shop&utm_source=adsite&utm_medium=banner, isn’t it? And what if you need to print that in a newspaper or other advertisement? A user wouldn’t bother typing in the tracking code, they would stop right after product-123 (if they even visited at all!) and then your analytics report would only record this as a direct visit with no other info tied to it. If you redirect the short link, the vanity url, ‘www.domain.com/buythis‘ to the longer, campaign tagged, url with a 301 redirect then you have made it easier for a user to type the url plus you have the benefit of being able to tag it with the values for GA to track so that you can get the information you want about that visit – that may be from an email or a banner or simply a link you have on your Facebook page, offline or online medium can be tracked like this.

Permanently Moving a Page with a 301 Redirect

The Scenario

If you have a page on your site – lets say for the sake of ease that it’s a simple html file but this is still applicable if you use a CMS that pulls content from a database – and you want to move it to a different section of the site then all you need to do is rename or move that html file (or use your CMS to copy the content to the new address) and your done… The Problem But what about the people who visit the old url? They will see an error page and that page looses it’s PageRank and the benefit of the links pointing to it. By using a 301 redirect all the authority passes onto the new page because the old one has permanently redirected to it. The old url could be on printed medium you can’t change – like business cards or leaflets – and it will be linked on other pages around your site and the web. If they visit the old link all they’ll see is a nasty 404 page not founderror or your custom error page (you should always set-up custom error pages), – unless you’ve replaced the moved page with another one that is. Note that 301 redirect and 404 page not found both start with numbers. These are status codes that are used to determine things about the page – like 301 meaning the page has moved permanently and 404 meaning the page is not found – and there are many more of them for you to learn about.

The Solution is a permanent Redirect

By redirecting the old url to the new one using a permanent 301 redirect in your .htaccess file anyone who visits it will be sent to the new page, avoiding the error, and the authority of the old page will be passed to the new one.

How to Code a 301 Redirect

Code is something that people like to avoid like the plague. I don’t know why, I love code. Looking at it, editing it, learning from it – it’s where most of my time gets spent. The normal everyday website owner hates having to play with site code because there’s always the chance that they might break something and not be able to fix it. This kind of coding is really simple – it can almost be done using a copy/paste formula – so I highly recommend that you don’t shy away from it before you try it.

The syntax of a 301 in .htaccess

You need to tell the server that you want to redirect a page and give it a redirect type. We are using a 301 redirect. This is done by simply saying redirect 301. Then we need to tell the server what page we want to redirect from – just put a space after ‘redirect 301’and a slash followed by the page like so: redirect 301 /buythis – and then the page we want it to redirect to in the same format. The whole code is shown below.
redirect 301 /buythis http://www.domain.com/store/products/prodict-123?utm_campaign=shop&utm_source=adsite&utm_medium=banner

In Conclusion

All you need to do with the code above is to change the source and destination url and paste is onto a new line in your .htaccess file. Hopefully now you feel confident enough to use 301 redirects on your site when you move things around or if you want to create vanity urls, to make pages easier to find or the address more user-friendly, and to track your marketing efforts through GA campaign tracking ability.


Notice: WP_Query was called with an argument that is deprecated since version 3.1.0! caller_get_posts is deprecated. Use ignore_sticky_posts instead. in /var/www/html/wp-includes/functions.php on line 4080