Tuesday 18 September 2012

htaccess writing for websites


A .htaccess (hypertext access) file is a directory-level configuration file supported by several web servers, that allows for decentralized management of web server configuration.
The original purpose of .htaccess - reflected in its name - was to allow per-directory access control, by for example requiring a password to access the content. Nowadays however, the .htaccess files can override many other configuration settings including content type and character set, CGI handlers, etc.
These files are placed inside the web tree, and are able to override a subset of the server's global configuration for that directory, and all sub-directories.
With the help of htaccess we can even block users from different ip's.
Iam here describing some of the basic htaccess examples :-


1)Redirecting index.html to index.php

RewriteEngine On
RewriteRule index.html  index.php

Here index.html in url is redirected to index.php

2)Redirecting services-servicename-8.html to services.php?id=8

RewriteEngine On
RewriteRule services-([a-zA-Z0-9\-]+)-([0-9]+).html services.php?id=$2 [NC]

3)Redirecting services-8-servicename.html to services.php?id=8


RewriteEngine On
RewriteRule services-([0-9]+)-([a-zA-Z0-9\-]+).html services.php?id=$1 [NC]

Look at the examples of 2 and 3

In the 2nd rule is explained like :- if a url comes like services-any keywordname-number.html,then it shoul redirect to sevices.php?id=number in the second partition, because id is provided their.

In the 3rd rule is explained like :- if a url comes like services-idnumber-sevicename.html then it shoul redirect to services.pho?id=number in the first partition, because id is provided there.


if url is like  services-([a-zA-Z0-9\-]+)-([a-zA-Z0-9\-]+)-([0-9]+).html 

then redirecting url shoul be like services.php?id=$3 [NC]

$3-> because id is provided in the third partition

4) Redirecting a folder to another folder

RewriteEngine on
RewriteRule ^(.*)/category-old/(.*)$ $1/category-new/$2 [R,L] 
Here request for folder named category-old should redirect o category-new folder


No comments:

Post a Comment