Create .htaccess Files Online

The .htaccess Editor allows you to easily create .htaccess files online. This is a great service for beginners just starting out or a seasoned developer looking to save some time. .htaccess files are not only very useful and helpful, but they are often essential for websites hosted on a shared host. Unfortunately they are somewhat cumbersome to understand. There are a lot of websites and references available to help you understand the files and syntax and available features, but this website makes it east by creating the syntax for you. I have listed the features available below.

Features

  • Deny all access to files
  • Basic authentication
  • Error page
  • Default page
  • Setup WWW
  • Redirect directives
  • Access restriction

The form is very easy to understand, which allows you to get started right away. Once you fill in the form, it will write the appropriate entry you will need in your .htaccess file. Not only is it easy to use, but it is a learning resource as well. You can gain an understanding for .htaccess files by simply looking at what is created by the form based on your parameters. Once you have created a few of these files, you won't need a lot of help in creating future files. But for now, take advantage of this great tool.

Easily Create Custom Error Pages

Chances are we've all been to a web page that no longer exists and have been staring at an error page that looks plain and provides virtually no useful information (other than the warning saying the page no longer exists). That's because Apache has included hard coded error pages in its httpd server, and while they get the job done, the job could be done better in just a few small steps. Custom error pages can be developed to help provide more information about the error, to automatically redirect the viewer to a landing page, and to integrate your site design.

Apache's ErrorDocument Directive can be used to display the default page packaged with the server or show custom error pages tailored specifically for your site. The 5 most common custom error pages used are listed below.

  • 400 - Bad Request
  • 401 - Authorization required
  • 403 - Forbidden
  • 404 - Wrong page
  • 500 - Internal server error

Creating custom error pages can be accomplished simply by creating a web page for each (or one web page accepting dynamic input) and adding a few lines to your htaccess file. I have listed examples for a static page for each error and a dynamic page for all errors below. This code should be added to your htaccess file.

Static Pages

ErrorDocument 400 /error/custom_400.html
ErrorDocument 401 /error/custom_401.html
ErrorDocument 403 /error/custom_403.html
ErrorDocument 404 /error/custom_404.html
ErrorDocument 500 /error/custom_500.html

Dynamic Page

ErrorDocument 400 /error/custom_error.php?error=400
ErrorDocument 401 /error/custom_error.php?error=401
ErrorDocument 403 /error/custom_error.php?error=403
ErrorDocument 404 /error/custom_error.php?error=404
ErrorDocument 500 /error/custom_error.php?error=500

Each of these pages could be stored wherever you like. I used the "error" directory as an example, and you can name the files however you like, using whatever type of file you wish. The ErrorDocument directive allows you to redirect to an external URL as well as a local path.

Apache mod_rewrite Basics

Apache mod_rewrite is a tool that I wish I had tried to understand a lot early than I did. It makes URLs look so much cleaner not to mention the positive SEO results. This capability can take a URL that is common in PHP that looks like: http://www.yoursite.com/users.php?name=testuser&mod=settings and turn it into a URL that looks like: http://www.yoursite.com/users/testuser/settings.

The basics are fairly easy and straight forward. You will need to create a .htaccess file in the directory you want to format the URL. I always include in the first line of the file "RewriteEngine on". You can use "RewriteEngine off" to disable blocks of rules so that you don't have to comment them out or delete them from your .htaccess file. The RewriteRule directive uses Regular Expressions to match the URL typed to the URL you want.

Example:

RewriteEngine on
RewriteRule ^my/account([/]*)$ myacct.php
RewriteRule ^news/([0-9]+)$ news.php?id=$1

Explanation:

RewriteRule ^my/account([/]*)$ myacct.php: This rule looks for a URL starting and ending with "my/account" after the website, for instance http://www.yoursite.com/my/account and then changes it on the server side to be http://www.yoursite.com/myacct.php, but the user only sees http://www.yoursite.com/my/account.

RewriteRule ^news/([0-9]+)$ news.php?id=$1: This rule looks for a URL starting with "news" after the website. After "news" it looks for a number that corresponds with the id generated with the news posting. The "+" allows for one or more digit to be entered. For instance http://www.yoursite.com/news/1 would be changed on the serve to http://www.yoursite.com/news.php?id=1 , but the user only sees http://www.yoursite.com/news/1.

I hope this helps get you started. This is just a basic tutorial and there is so much more that can be done with Apache's mod_rewrite feature. For more in depth information, check out these resources: