How to create .htaccess file

What is htaccess?

htaccess is short for Hypertext Access and is a configuration file used by Apache-based web servers that controls the directory that it lives in as well as all the subdirectories underneath that directory. These are hidden plain text files that are on the server to help control how your visitors interact with your website.

Using .htaccess file, we can easily configure and redirect webpages on the server and create friendly URLs, sub domain directory re-directions and many more.

For example

  • Request on can be used to open
  • Request on can be used to open

.htaccess can also be used to hide URL key parameters.

  • can be be used to open

These modifications in the URLs are done primarily to make them user friendly and nice looking. Any good website with decent following uses htaccess files for redirection and modification of URLs.

Creating a plain .htaccess

  1. You can create a plain .htaccess file using a text editor like Notepad.
  2. Create a blank notepad file and save it as htaccess.txt. Do not save it as .htaccess, as the system will hide it.
  3. Use FTP to upload the file to the exact directory you want to modify. Usually this is the public_html folder.
  4. Once the file is uplaoded, rename it to .htaccess exactly.
  5. If your file disappears, check your FTP’s settings to view hidden files.

Blank htaccess

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTP_USER_AGENT} libwww-perl.* 
    RewriteRule .* – [F,L]

Explanation of code


The .htaccess file starts and end with the IfModule.


You need RewriteEngine On in each of the htaccess files that have rewrite rules, but only once. Otherwise the rules will be ignored.


There is a threat called libwww-perl (LWP). It is a WWW client/server library for perl which helps lot of hacker, spammer, bots to perform attacks on your website. In order to block the perl attacks the following code is added to the htaccess after RewriteEngine On.
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*



Chain – if a rule matches, then processing continues as usual – the flag has no effect. If the rule does not match, then all following chained rules are skipped.


Forbidden – This forces the current URL to be forbidden – it immediately sends back a HTTP response of 403 (FORBIDDEN). Use this flag in conjunction with appropriate RewriteConds to conditionally block some URLs.


Gone – This forces the current URL to be gone – it immediately sends back a HTTP response of 410 (GONE). Use this flag to mark pages which no longer exist as gone.


Last Rule – Stop the rewriting process here and don’t apply any more rewrite rules. If you created a loop with ReWrite it will cause processing to be repeated starting from the first RewriteRule. The [L] flag terminates rewriterule processing only if the current rule is invoked.


Next – Re-run the rewriting process (starting again with the first rewriting rule). This time, the URL to match is no longer the original URL, but rather the URL returned by the last rewriting rule. Be careful not to create an infinite loop!


No Case – This makes the Pattern case-insensitive, ignoring difference between ‘A-Z’ and ‘a-z’ when Pattern is matched against the current URL.


Or – If it matches this condition or the next.

Final htaccess for demo application

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTP_USER_AGENT} libwww-perl.* 
    RewriteRule .* – [F,L]

    RewriteRule ^/?$ public/index.php [NC,L]
    RewriteRule ^show/?$ public/show.php  [NC,L]
    RewriteRule ^blog/([a-z0-9]+)/([a-z0-9]+)/?$ public/blog.php?name=$1&id=$2 [NC,L]

Adding URLs to htaccess

  1. RewriteRule ^/?$ public/index.php [NC,L]
    This rule redirects any request to to the index.php located in the public folder.

  2. RewriteRule ^show/?$ public/show.php [NC,L]
    This rule redirects any request to the URL to show.php located in the public folder.

  3. RewriteRule ^blog/([a-z0-9]+)/([a-z0-9]+)/?$public/blog.php?name=$1&id=$2 [NC,L]
    A URL like This is same as[‘name’]&id=GET[‘id’] where there are two GET parameters (name and id).

Online Demo

index htaccess

blog htaccess

show htaccess

Please feel free to download the source code for reference.

View Demo Download source

This content has been helpful to you?

Thanks for contributing!

Yes No