A Simple Guide to .htaccess
This YouMoz entry was submitted by one of our community members. The author’s views are entirely their own (excluding an unlikely case of hypnosis) and may not reflect the views of Moz.
The .htaccess file on mod_rewrite enabled servers (usually Unix-based but sometimes decent Windows hosts) can be an SEOs dream (and sometimes nightmare). The small file that should be found in the root folder is what you will use to fix a lot of the crawling errors with rewrites and redirects. On small, static sites, they can even be used to create more usable, pretty URLs.
An introduction to .htaccess
.htaccess files are written in the Apache programming language. For this simple tutorial, you won't need to know a huge amount about the language, but you should know that it can be very frustrating. Not because it's a difficult language to know, but because it doesn't provide any descriptive errors and will just give a page styled the same as a server error. A subtle mistake in a .htaccess can cause a large amount of downtime if you can't resolve the issue quickly, so keep that ctrl-z handy and always keep backups if necessary.
Comments
One thing that I was never taught to do when I started designing websites was to comment. Up until recently, it had never crossed my mind that commenting was helpful, but when it comes to reusing and re-editing code it is essential.
Editing out lines on a .htaccess file is the same as you would in your hosts file: simply add a # to the beginning.
Samples
Preventing Canonicalisation - dubdubdub or not
Canonicalisation is something very relevant, even Matt Cutts recently revealed that a load of Google websites have canonicalisation problems:
The most common usage of the .htaccess file is associated with canonicalisation. Just three lines of code will help to prevent potential indexation and duplicate content problems.
The three lines of code will force a "www." to prefix your URL, regardless of what URL has been put in. Whether it's http://yourdomain.com/ or http://yourdomain.com/dinosaur/, then will automatically change to http://www.yourdomain.com/ and http://www.yourdomain.com/dinosaur/ respectively.
Please keep in mind that this will just help to prevent potential issues, it won't prevent them completely. Always use the canonical tag where applicable and not just rely on the .htaccess file.
Fixing Crawling Errors
As a webmaster, you should have Google's Webmasters Tools set up so you can take a closer look at your site when it comes to Google. If you haven't got this set up... DO IT NOW!!! If you do, then head to your Dashboard and look in the top right hand side, you will see a table called "Crawl Errors". This table contains all of the issues that Google has found whilst crawling your site (or not crawling it if there are a lot of errors).
These errors can and should be fixed as soon as your notice them. The only errors that can really be fixed using simple .htaccess are "Unreachable" and "Not Found", the former only being the odd one or two, "Unreachable" pages are those that usually need some added attention outside of the .htaccess file.
For the time being, I'd stick with fixing the "Not Found" crawling errors, this is very easy to fix, all you need is to use a 301 redirect.
Who said that 301 redirects were hassle?
Creating Pretty URLs
In my opinion, a pretty URL is much more user-friendly and makes your site appear more professional and 'clean'. This is something that will likely only work on static sites, most dynamic sites and content management systems will have their own, more complex methods of creating pretty URLs.
http://www.yourdomain.com/not_so_pretty.html --> http://www.yourdomain.com/sexy-url/
The method I'm showing you is essentially making the server think that your files are folders, so you should make sure to check that your navigation etc will still work, all images will be displayed correctly etc. Whether this is relative or absolute, it's up to you.
The [L] should not be used at the end of every line, just the end of the last RewriteRule that you are doing.
Conclusion
The .htaccess file is one of the most powerful tools in an SEOs arsenal (for on-site SEO). It is essential that any webmaster is aware of the file and can at least do the above when it is essential.
If I've missed something out or made a mistake, then I'm only human. Please comment below and I will amend & update. You can also find me on twitter (@lukejonesme) if you want a chat!
Comments
Please keep your comments TAGFEE by following the community etiquette
Comments are closed. Got a burning question? Head to our Q&A section to start a new conversation.