Htaccess Magic: 4 Tips for a Better Website

 Congratulations! You have a website! Now that you have one, you also have the responsibility of taking care of one. Website optimization is not always easy, but it can be with basic knowledge of how they work. One tool you can use to optimize your website is an .htaccess file.

Web servers that use Apache HTTP Server to publish websites have an override feature that allows web hosting users, even ones with shared hosting accounts, to change some web server settings on a local level. If the host has the AllowOverride setting enabled on the web server, the user can use .htaccess files to enable any desired Apache directives. The following are four directive types that can bring a little magic to your website.

1. Custom error docs

When a user mistypes a URL to a page on your site or visits one for a page that no longer exists, one of two things will happen. Either the user will end up at a plain white page with the words “404 Error. File Not Found” or at an error page created by your web host, promoting their services. Neither is good for your services or your site.

By creating custom error documents, you can let users know they did actually make it to your site but might have made a wrong turn. You can also use it to suggest a better starting point for them to navigate through your site.

To create custom error docs, the first step is easy. Make normal HTML files for each error you want to include. You can find a list of Apache error codes on the web.

The next step is to create a file called .htaccess and place it in the root directory of your website – the directory that holds your HTML files. In the .htaccess file, place the error code redirects:

ErrorDocument 500 http://your-domain.tld/docs/500error.html
ErrorDocument 404 http://your-domain.tld/docs/404error.html
ErrorDocument 403 http://your-domain.tld/docs/403error.html
ErrorDocument 401 /local/path/401error.html

As you see, you can use a local path or full URL to each error HTML file that you have created.

2. Rewrite rules

You can use rewrite rules for a wide variety of purposes, the most common purpose is for search engine optimization. Dynamic web apps use very long URLs, and you can use Apache’s mod_rewrite engine to make them shorter. If you are running a content management system like WordPress or Joomla, it will likely add the correct rewrite rules to your .htaccess file for you. If, however, you are creating your own web app, you should also create some rewrite rules.

Rewrite rules follow a simple pattern:

RewriteRule Pattern Substitution [Flag]

For example, a simple rewrite rule is:

RewriteRule ^killer-whales.html$ orcas.html

In this example, people who access the page “killer-whales.html” will be redirected to the actual page: “orcas.html”. Apache also offers many more rewrite options you can try to make your site easier to access.

3. Restrict access

Htaccess files can also have security benefits. One example is that you can restrict access to a particular file or directory. For example, to deny access to a single IP address, you would enter:

Order Allow,Deny
Deny from 155.55.555.5.
Allow from all

Anyone attempting to access your site from that IP address will be denied. More information about mod_access is available in the Apache documentation.

4. Allow or Prevent Directory Browsing

When an index file is not present in a directory, Apache will display the contents of the directory. Many file repositories will use something like this by default to make it easy for people to browse the files. If, however, you do not want people to browse the files in a directory, you can easily disable browsing.

Options All -Indexes

On the other hand, if your web host has disabled browsing but you want it enabled, you do so with this directive:

Options All +Indexes

Taking Control

The whole point of .htaccess is to give the user some limited control over the server without affecting the other users who share it. This is only possible if your web host has enabled the “AllowOverride” setting in the web server’s configuration. The best hosting providers usually do, but even if yours does not, you may be able to convince them to make special arrangements for you. It will make your website better and make your job as web administrator easier.

Guest Author Tavis J. Hampton is the author of the upcoming book KDE for the Graphical User. You can find more of his writing at TavisOnline.com.

2 thoughts on “Htaccess Magic: 4 Tips for a Better Website”

  1. Thanks a lot for the advices. Redirecting 404 and ather error pages to a content page can bring lost visitors back on the track. In my opinion this is one of the most important .htaccess settings, ve all lose pages in time.

Leave a Reply