Spiga

Apache Mod_Rewrite: Basics & rule writing

Rewrite module one among the powerful tools available with apache to get most of the configurations by-passed and have the sites function like desire. Here are the basics for this:

Expressions in use:

^ represents the start of a string as in case of any other Linux programmes.

$ represents the end of the string or to denote the end of a string.

. Any single character.

(a|b) represents the method for comparison.. i.e, a or b

(a,b,c) Group a section for use

[abc] define the item in range for here it will be a b or c

[^abc] defines the item NOT in range a b or c

a? experssion will count a zero or any one having 'a' as positive results

a* will check for zero or more of a

a+ will check for 1 or more of a

a{3} will check for exactly 3 of a

a{3,6} will check for values between 3 and 6

!(pattern) will check for the details when the patterns do not match.


Redirection Header Code

Here are the normal codes used up for apache to represent errors/warning messages. These can be used in the .htaccess redirection rules to have the required results displayed.

301 redirect permanently
302 redirect permanantly
403 forbidden entries
404 not found
410 Gone

Rewrite Rule Flags
The following set are characters and patterns used for defining rules via .htaccess.

R[=code] redirect to a url using optional coding entries.
F sends out a forbidden header message.
G sends out a "no longer exist" message.
P used to represent proxy entries
L to represent last rule
N to represent next rule or to restart the rules
C to represent the chain of rules
T= is used to define mime types
NS is used to skip the request if it is a internal sub request
NC to define if the entry is case sensitive
QSA to append the query string
NE to define " do not escape" output
PT is known as passthrough
S=x is used to skip x rules
E=var:value used to set environmental variables

Condition Flags
NC -
Case sensitive entry
OR - To define the conditional entries.

Format of variables used
Here is the format of the variables available.

%{name of the variable}

The common list of variables in use are:

HTTP_USER_AGENT
HTTP_REFERER
HTTP_COOKIE
HTTP_FORWARDED
HTTP_HOST
HTTP_PROXY_CONNECTION
HTTP_ACCEPT

The details of requests in use are:

REMOTE_ADDR
REMOTE_HOST
REMOTE_USER
REMOTE_IDENT
REQUEST_METHOD
SCRIPT_FILENAME
PATH_INFO
QUERY_STRING
AUTH_TYPE

Server variables available are:

DOCUMENT_ROOT
SERVER_ADMIN
SERVER_NAME
SERVER_ADDR
SERVER_PORT
SERVER_PROTOCOL
SERVER_SOFTWARE

Time variables available are:

TIME_YEAR
TIME_MON
TIME_DAY
TIME_HOUR
TIME_MIN
TIME_SEC
TIME_WDAY
TIME

Special variables in use are:

API_VERSION
THE_REQUEST
REQUEST_URI
REQUEST_FILENAME
IS_SUBREQ

Directives in use are:

RewriteEngine
RewriteOptions
RewriteLog
RewriteLogLevel
RewriteLock
RewriteMap
RewriteBase
RewriteCond
RewriteRule

These are the main variables and details used for defining rules sing apache mod_rewrite to carryout modifications to site without accessing or modifying the main apache configuration

Here are some of the common rules used to define how the variables can be used with rewrite rule:

1. When the site is permanently redirected to a different url:

RewriteCond %{HTTP_HOST} ^www.domainname.com$ [NC]
RewriteRule ^(.*)$ http://www.newdomainname/$1 [R=301,L]

2. Page when moved temporarily:

RewriteRule ^page.htm$ new_page [R, NC, L]





0 comments: