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 http://example.com can be used to open http://example.com/public/index.php.
  • Request on http://example.com/admin/ can be used to open http://example.com/admin/login.php.

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

  • http://example.com/blog/10/ can be be used to open http://example.com/public/blog.php?id=10

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]
</IfModule>

Explanation of code

IfModule

The .htaccess file starts and end with the IfModule.

RewriteEngine

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

Libwww-perl-attack

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.*

Flags:

[C]

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.

[F]

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.

[G]

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.

[L]

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.

[N]

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!

[NC]

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]

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]
</IfModule>

Adding URLs to htaccess

  1. RewriteRule ^/?$ public/index.php [NC,L]
    This rule redirects any request to http://example.com/ 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 http://example.com/show/ 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 http://example.com/blog/Tutorial1/21/. This is same as http://example.com/blog?name=GET[‘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