What is htaccess

person shubham sharmafolder_openhtaccesslocal_offeraccess_time February 8, 2017

Htaccess (HyperText Access) is a simple configuration file that allows designers, developers and programmers alike to alter the configuration of the Apache Web Server in order to provide additional functionality. Such functionality can include redirecting users, URL re-writes and providing password-protected directories; but it can do so much more.

So let’s begin …

Creating and Uploading an .htaccess File

Creating an .htaccess file is very easy.

Simply open Notepad or a similar text-based program, switch off word-wrap, add the code and save the file in the usual way.

For example, you could call it:

Upload the file to the relevant directory on your web server and then rename it like so:

Remember, the .htaccess file should be using 644 permissions and uploaded in ASCII mode. If your .htaccess file does not work, then you should contact your system administrator or web hosting company and ensure they have enabled ‘.htaccess’ within your account, as some web hosting companies do not allow its use without prior permission. Unfortunately, .htaccess will not work on Windows-based servers.

Using .htaccess

It is important to remember that an .htaccess file will affect the directory it is placed in and all resulting sub-directories. Therefore, if you add your ‘.htaccess’ file to the ‘web site root’ then it will affect all subsequent folders like so:

However, if you place the ‘.htaccess’ file in http://www.yourdomain.com/directory1 then the features of the ‘.htaccess’ will be restricted to that folder and all child folders only. For example:

After editing your .htaccess file on multiple occassions it may look a little complicated so I would recommend implementing comments. To do this, simply place the hash symbol at the beginning of every line like so:

Useful Snippets

And to get you started, it’s snippet time …
(although one or two of them are strictly directives for Apache)

Directory Index

You can change a default index file of directory with:

Custom Error Pages

You can redirect your users to an error page with:

And you can extend this like so:

But remember to create your error pages!

Remove the Need for www in Your URL

Keep your site consistent by removing the need for ‘www’ by using:

Set the Time Zone for Your Server

Control Access to Files

Most people will remember that .htaccess is most often used to restrict or deny access to individual files and folders and you can do this like so:

However, if you would like to be more specific and ban a specific IP address then you could use:

or alternatively for several IP addresses, you could use:

301 Permanent Redirects

Worried about those old links? Then try:

Set the Email Address for the Server Administrator

By using the following code you can specify the default email address for the server administrator:

Detecting Tablets and Redirecting

If you would like to redirect tablet-based users to a particular web page or directory, try:

Link Protection

Concerned about hotlinking or simply want to reduce your bandwidth usage? Try experimenting with:

Force “File Save As”

If you would like force users to download files rather than view them in the browser you could use:

or you simplify this as:

Rewrite URLs

If you would like to make your URLs a little easier to read (ie changing content.php?id=92 to content-92.html) you could implement the following ‘rewrite’ rules:

Redirect Browser to https

This is always useful for those who have just installed an SSL certificate:

Activate SSI

If you want to activate SSI for HTML and or SHTML file types, try:

Disable or Enable Directory browsing

Change the Charset and Language headers

For those who want to change the current character set and language for a specific directory use:

Block Unwanted Referrals

If you want to block unwanted visitors from a particular website or range of websites you could use:

Block Unwanted User Agents

With the following method, you could save your bandwidth by blocking certain bots or spiders from trawling your website:

Block Access to a Comprehensive Range of Files

If you want to protect particular files, or even block access to the .htaccess file, try customising the following code:

And Lastly …

For reasons of security alone, I think the chance to rename the .htaccess file is very useful:

warningComments are closed.