X

How to get the most of your .htaccess file

The .htaccess file has been around since the early days of the Internet and is still in use on millions of web servers. While most users don’t use it, it’s a really powerful tool, which can help you a lot in many different situations. This is why, it is vital for any webmaster to get to know what they can do with an .htaccess file.

With an .htaccess file, you can quickly restrict the use of images on your website from other sites, add a custom 404 page, block an IP address, redirect a web page to a different web page, etc.

Today, we will show you how to do all those things with the Web Hosting Control Panel.

Hotlink Protection – go to Advanced -> Hotlink Protection. From there, simply select a website and the exact path, if you want to protect a specific folder only. Here is the code for doing it manually:

RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?my-website\.com/.*$ [NC]
RewriteRule \.(gif|jpg|swf|flv|png)$ http://www.my-website.com [R=302,L]

Password Protection – go to Advanced -> Password Protection. Pick a website and select the folder you need to protect – you will need to set a username and a password. At any moment later on, you can edit the password or remove the password protection entirely. To do it manually, use the following code:

<Files "login.php">
Order deny,allow
Deny from all
AuthName "htaccess password prompt"
AuthType Basic
AuthUserFile /web/askapache.com/.htpasswd
Require valid-user
</Files>

Please note that this is just an example and the path displayed above may be different for your particular hosting account.

Keep in mind that you will also need to create an .htpasswd file with the password inside. It has to be stored in the same folder as the .htaccess file. Don’t enter the password in plain text – use MD5 encryption.

Here is an MD5 encryption tool you can use – http://www.md5online.org/md5-encrypt.html.

Custom Error Pages – go to Hosted Domains and right-click on any of your hosts. Select Edit. Now you will see the most popular error pages. They are currently set to Default, but you can pick a custom page – simply select the Custom URL option and enter the URL of the 404 page that you want to use. To manually add an error page, use the following code:

ErrorDocument 404 /path/to/error/page.html

IP Blocking – in your Hepsia Control Panel, go to Advanced -> IP Blocking. From there you can quickly block a specific IP from accessing a given website. Additionally, you can use the following code in your .htaccess file:

ErrorDocument 403 http://www.my-website.com/
Order deny,allow
Deny from all
Allow from 107.45.18.3

This is just a small list of what can be done with an .htaccess file. The folks from AskApache have prepared a very detailed list of exampleshttp://www.askapache.com/htaccess/htaccess.html. Be sure to check the code snippets – they are frequently updated.

Don’t forget to also have a look at our .htaccess generator, which is located in the Advanced section of the Hepsia Control Panel. It will generate a custom .htaccess file with all the options that you want to use.

Keep in mind that if you are using a script-based website (a WordPress-based website, a Joomla-based website, etc), you already have an .htaccess file, which has been generated automatically by the application. Instead of generating a new one, you are better off editing your existing .htaccess file.

admin: