10 htaccess Hacks Every SEO Should Know

by Kevin Spence on October 6, 2009

There’s a lot that you can do with an htaccess file, and of course, things can get pretty advanced in a hurry. Here, we’re going to keep things pretty simple. These are the 10 basic htaccess hacks that every webmaster should know.

1. Force Caching with htaccess

Use: The following htaccess code won’t help the initial pageload, but it will significantly help subsequent pageloads by sending 304 status when requested elements haven’t been modified.

FileETag MTime Size
ExpiresActive on
ExpiresDefault “access plus x seconds”

I generally set the cache for one day (86400 seconds). You can also set different expirations for each file type by breaking each file type up into separate ExpiresByType lines such as:

ExpiresByType image/gif “access plus x seconds”
ExpiresByType text/css “access plus x seconds”

Simple!

2. Set a Custom 404 Page with htaccess

Use: I think this one is self explantatory. Just change ‘/notfound.html’ to match the path to your custom 404 page.

ErrorDocument 404 /notfound.html

3. Implement a 301 Redirect with htaccess

Use: If you have permanently changed the URL structure on your site (via either optimization change or CMS migration), you will want to implement 301 redirects from the old URL to the new URL.

The syntax for a basic 301 redirect is:

Redirect 301 relative/path/to/oldurl/ http://www.domain.com/newurl/

Explanation:
The first URL should be a relative path to the old URL and the second one should be an absolute path to the new URL.

4. Only allow specific IP addresses in certain directories

This is especially useful for admin directories. I generally set my home IP and work IP as the only allowable IPs who can even attempt a login. Unlike other .htaccess hacks, this one doesn’t work from the root folder. You will need to create a new .htaccess file, put the following code in it, and upload it to your admin directory.

AuthUserFile /dev/null
AuthGroupFile /dev/null
AuthName “Example Access Control”
AuthType Basic

order deny,allow
deny from all
allow from xx.xx.xx.xx

To allow a second IP, just add another ‘allow from’ line.

5. Prevent Image Hot Linking with htaccess

Removed. Here is a MUCH better way to go about this thanks to Maurizio Petrone

6. Enable gzip with htaccess

Gzip is a means of compressing the files on your server so they will load faster. To enable gzip, just

AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/javascript text/css application/x-javascript
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip

BrowserMatch bMSIE !no-gzip !gzip-only-text/html

7. Remove ‘category’ from a URL with htaccess

Many content management sytems use the ‘category’ URL segment. For instance:

http://makeitrank.com/category/category-name

Well, that’s fine, and it’s necessary to make the CMS work the way it’s supposed to, but it doesn’t need to be visible to do its job. Just drop the following code into your htaccess file to get rid of it.

RewriteRule ^category/(.+)$ http://www.yourdomain.com/$1 [R=301,L]

8. Define any page as the home page with htaccess

You can set any page as the homepage by adding the following to the htaccess file in your root directory.

DirectoryIndex myfile

9. Disable Directory Browsing

You want to keep people out of any directories that might reveal security weaknesses — for instance, plugin directories.

You can block vistors from browsing the directories by adding the following line to the htaccess file in the directory you’d like to block:

Options All -Indexes

10. Protect your htaccess file

Lastly, you want to protect your htaccess file. Hopefully your host has protected it for you, but you can’t be too safe with these things. The following hack will prevent anybody from accessing your htaccess:

<Files .htaccess>

order allow,deny
deny from all

</Files>

Are there any useful htaccess hacks I’ve left out? Let me know!

{ 19 comments… read them below or add one }

1 Sharon Hayes January 6, 2010 at 7:29 pm

Nice list! Thanks for putting it together!

2 Matt January 7, 2010 at 3:33 am

A very informative list, I would have liked to have read something like this years ago!!

3 SEO Scene January 7, 2010 at 3:52 am

Great! Nr. 10 was really new for me. More suggestions for Nr. 3 you can found under the URLWebsite.
regards
Loewenherz

4 Kenneth Dambo January 7, 2010 at 5:05 am

Nice and simple explanation of quite usefull uses of htaccess. Many htaccess guides get quite complicated very quickly as mentioned in the beginning.

5 Tom Ruthemann January 7, 2010 at 5:48 am

Very helpful hints. Much appreciated, thanks.

6 Matthew Pack January 7, 2010 at 6:48 am

would be great to see some more of these

7 Tea Embassy January 7, 2010 at 7:10 am

Hey thanks – I didn’t even know you could set Expire headers via .htaccess.
Just implemented this in our website!

8 toputop January 7, 2010 at 8:21 am

Good explanation on .htaccess. Very useful one.

9 Ryan January 7, 2010 at 10:10 am

These are neither hacks, or really related to SEO at all…

10 Edwin January 7, 2010 at 10:18 am

These are great!
One question about number 6. All the files should be gzip-ed before doing that? or this will do it for you?

11 Chole Ray January 7, 2010 at 11:42 am

Thanks for the great tips. I will most definitely be using some of these.

12 1975jmr 侯壮马 January 7, 2010 at 12:26 pm

Usefull tips, thanks.
Have a good day ;-)

13 Keiron @ BetterWebSpace January 7, 2010 at 12:40 pm

A great list there, some we use every day and others we have to look up every time we actually want to use them :)

14 Dwayne January 7, 2010 at 1:07 pm

Thanks a ton!

15 Miles January 7, 2010 at 2:17 pm

Some useful ideas, a few of which I didn’t know about – but this guide is not much use. The lack of/inconsistent formatting of the code makes it difficult to work out what should be used and what is optional.

Also some things such as #1 really should have example code, instead of “access plus x seconds” – I have no idea what I would replace this with for a real situation.

There is no indication of where in the .htaccess these rules should go to work properly and not conflict with other rules.

Finally, wouldn’t #10 used as is block access to the entire site?

16 Johnny Ace January 7, 2010 at 3:19 pm

Awesome tips. The tip on disabling directory browsing is especially useful, since even Google can (and will) index directories you do NOT want indexed: WordPress plugin dirs for instance.

17 Brett January 7, 2010 at 6:16 pm

Great list, thanks. Perhaps you might want to include how to redirect all pages to a certain page, except for certain IP addresses (i.e. taking your site down for maintenance and redirecting users to one page, but allowing yourself to view all files).

18 Kevin Spence January 7, 2010 at 9:21 pm

@Ryan — My philosophy is that every element of a site is related to SEO, even if it doesn’t have anything to do with linkbuilding or traditional SEO practices. I understand what you mean, though.

@Johnny Ace & Brent — Good ideas. Maybe I’ll write a follow-up post and include those. Both are great ideas.

19 Kevin Spence January 7, 2010 at 9:25 pm

@Miles You’re absolutely right on the formatting & usability concerns. I’ll improve that.

As for #10, Wordpress formatted things a little wonky on me, and tried to render the opening brackets around the files .htaccess as HTML tags. My fault, I didn’t proofread carefully enough. It’s fixed now.

Leave a Comment

{ 8 trackbacks }

Previous post: My Google Sidewiki Entry

Next post: How to get in Google’s real time search results?