Saturday, November 6, 2010

Htaccess Tricks and Tips

SkyHi @ Saturday, November 06, 2010


Some people might not aware of the power of htaccess, I have 17 htaccess methods in this article which I have used or tested it before, and I think some of them are essential tricks and tips to protect your webserver against malicious attacks and other would able to perform simple tasks efficiently such as redirection and web server optimization.

Last but not least, if you have been looking for web hosting services, you might want to have a look at this unlimited web hosting. :)


The following htaccess will able to help you to achieve simple task such as redirection and web server optimization.

1. Set Timezone

Sometimes, when you using date or mktime function in php, it will show you a funny message regarding timezone. This is one of the way to solve it. Set timezone for your server. A list of supported timezone can be found here

2. SEO Friendly 301 Permanent Redirects

Why it's SEO friendly? Nowadays, some modern serach engine has the capability to detect 301 Permanent Redirects and update its existing record.

3. Skip the download dialogue

Usually when you try to download something from a web server you get a request asking whether you want to save the file or open it. To avoid that you can use the below code on your .htaccess file

4. Skip www

One of the SEO guideline is, make sure there is only one URL pointing to your website. Therefore, you will need this to redirect all www traffic to non-ww, or the other way around.

5. Custom Error page

Create a custom error page for each of the error codes.

6. Compress files

Optimize your website loading time by compressing files into smaller size.

7. Cache files

File caching is another famous approach in optimizing website loading time

8. Disable caching for certain file type

Well, in the other hand, you can disable caching for certain file type.


The following htaccess code will able to enhance the security level of your webserver. Hotlinking protection is pretty useful to avoid other people using images that stored in your server.

1. Hotlinking protection with .htaccess

Hate it when people stealing bandwidth from your website by using images that are hosted in your web server? Use this, you will able to prevent it from happening.

2. Prevent hacks

If you want to increase the security level of your website, you can chuck these few lines of codes to prevent some common hacking techniques by detecting malicious URL patterns.

3. Block access to your .htaccess file

The following code will prevent user to access your .htaccess file. Also, you can block multiple file type as well.

4. Rename htaccess files

You can also rename your .htaccess file name to something else to prevent access.

5. Disable directory browsing

Avoid the server from displaying directory index, or the opposite.

6. Change default Index page

You can change the default page index.html, index.php or index.htm to something else.

7. Block unwanted visitor based on referring domain

8. Blocking request based on User-Agent Header

This method could save your bandwidth quota by blocking certain bots or spiders from crawling your website.

9. Secure directories by disabling execution of scripts