A little about HTACCESS

Introduction.
htaccess is simply a server side configuration file, used for webserver configuration management. Which contain some instructions in a text file. And those instructions can be used to improve your server settings and do many other helpful things such as customize your own 404 page, rewrite long urls with more readable ones, disable directory listing, put security restrictions for a directory etc.

How to add htaccess file to Server:
What we will do is, just create a file with the name .htaccess and put it in our webserver directory. This file would contain some instructions (we call them rules ) in a pre-specified format, that webserver can understand. We would discuss about the rules and its structure in more detail.

Things we can acheive with htaccess rules:

  • Redirect / Rewrite urls
  • Password protection for directories
  • Deny visitors by IP address
  • Adding MIME types ( instruct the server how to treat different varying file types)
  • Disable directory listings
  • Preventing access to your PHP includes files
  • Prevent access to php.ini
  • Forcing scripts to display as source code
  • Ensuring media files are downloaded instead of played
  • Setting up Associations for Encoded Files
  • Preventing requests with invalid characters

Lets write a simple .htaccess file:
We will write a simple .htaccess file to show how we can redirect one url to another.
Some times our url will be long and complex to the user. In those times you can simply replace such long urls with more readable and search engine optimised urls .

This is how we acheive url redirection through htaccess rule.

RewriteEngine On
RewriteRule  ^myurl.*$  http://example.com/   [NC,L]

This rule in .htaccess file will redirect request http://www.example.com/myurl to http://www.example.com/

Explanation:

a general format of the rule for url redirect will be
Redirect  /old_url/   http://www.yourdomain.com/new_url [flags]

Now lets do cross section in the real example:
RewriteEngine On  : will turn on the rewriting engine. We will write this line only once in a
.htaccess file.

RewriteRule   ^myurl.*$    http://example.com/   [NC,L]

The above line specify the rule we want to apply.
^myurl.*$ :  The server will compare the url of all the requests to  this pattern. If it matches
then it will replace the pattern with the following argument in rewrite rule. ( here it is
http://www.example.com/ ).

[NC,L] : These are  “Flags”, that tell Apache how to apply the rule. “NC”, tells Apache that this rule should be case-insensitive, and “L” tells Apache not to process any more rules if this one is used.

Condition
Now lets try another example, To redirect an old domain to a new domain. That is if you have chnaged your website domain name to a new name with out changing its directory structure, you can put this .htaccess file in your webserver , which will redirect all requests to old domain to new domain.

RewriteEngine On
RewriteCond  %{HTTP_HOST}    old_domain\.com [NC]
RewriteRule  ^(.*)$    http://www.new_domain.com/$1 [L,R=301]

Here we have a RewriteCond (rewrite condition) preceded by RewriteRule. Which is the most common case in htaccess files. By using rewriteCond before rewrite rule we can limit the RewriteRule only if that condition is met. We can also write multiple condition using and, or.

How RewriteCond works?
General format:   RewriteCond server variable string

The RewriteCond operates in a similar way with RewriteRule. The string to test can be a variety of things. Such as which browser used, the IP address etc, it depends on the server variable we have specified. In the above example we have used the HTTP_HOST variable. It verifies whether the given string matches the host domain, and if it does do RewriteRule.

List of server variable that we can use:

HTTP Headers
HTTP_USER_AGENT
HTTP_REFERER
HTTP_COOKIE
HTTP_FORWARDED
HTTP_HOST
HTTP_PROXY_CONNECTION
HTTP_ACCEPT

Connection Variables
REMOTE_ADDR
REMOTE_HOST
REMOTE_USER
REMOTE_IDENT
REQUEST_METHOD
SCRIPT_FILENAME
PATH_INFO
QUERY_STRING
AUTH_TYPE

Server Variables
DOCUMENT_ROOT
SERVER_ADMIN
SERVER_NAME
SERVER_ADDR
SERVER_PORT
SERVER_PROTOCOL
SERVER_SOFTWARE

Dates and Times
TIME_YEAR
TIME_MON
TIME_DAY
TIME_HOUR
TIME_MIN
TIME_SEC
TIME_WDAY
TIME

Special Items
API_VERSION
THE_REQUEST
REQUEST_URI
REQUEST_FILENAME
IS_SUBREQ

List of flags that we can use:

C (chained with next rule)
CO=cookie (set specified cookie)
E=var:value (set environment variable var to value)
F (forbidden – sends a 403 header to the user)
G (gone – no longer exists)
H=handler (set handler)
L (last – stop processing rules)
N (next – continue processing rules)
NC (case insensitive)
NE (do not escape special URL characters in output)
NS (ignore this rule if the request is a subrequest)
P (proxy – i.e., apache should grab the remote content specified in the substitution section and return it)
PT (pass through – use when processing URLs with additional handlers, e.g., mod_alias)
R (temporary redirect to new URL)
R=301 (permanent redirect to new URL)
QSA (append query string from request to substituted URL)
S=x (skip next x rules)
T=mime-type (force specified mime type)

References:
http://www.addedbytes.com/for-beginners/url-rewriting-for-beginners/
http://perishablepress.com/stupid-htaccess-tricks/
http://www.htaccess-guide.com/
http://www.freewebmasterhelp.com/tutorials/htaccess

Advertisements