Apache rewrite is an apache web server module which gives opportunity to change URL folders during opening of pages. At first it is hard to understand mod_rewrite but if you know what you are doing then it will become a usefull tool. You can reach the documents supplied by developer site located here: Apache Docs

Usage areas of Apache mod_rewrite are:

  • URL Redirect Ex: Redirection of plugged.in to www.plugged.in
  • Browser Redirect Ex: If Internet explorer is below version 7 then redirection to basic folder instead of high technology site
  • Bot Redirect, Ex: Redirection of Google bot for SEO optimization to a page which contains static links
  • Redirection to a different sun folder regarding to the browser language
  • To block out creating hotlinks of picture files Ex: You can prevent consuming bandwidth which is done by pictures used by other sites
  • SEO SEO Optimizationu, Ex: To show dynamic pages like php, aspx as static like .html

used for purposes like above.

In order to use Apache Mod_rewrite, you must have the below instructions in the httpd.conf or in VirtualHost settings:

OPTIONS  FollowSymLinks
# or
OPTIONS All
#and
AllowOverride All
#or
AllowOverride FileInfo

You must create “.htaccess” file in the root folder of your web site in order to use mod_rewrite. There is always a point character in the begining of the secret files in the Unix systems, this is why htaccess can’t be seen through FTP applications. Because of this you should backup htaccess file locally if you only use ftp to store your files.
.htaccess must begin with this description:

# Apache must know the URLs which we change.
RewriteEngine On
# After that line, we will write the URLs to change

Rewrite works like an algoritm, fist of all we should tell rewrite to change which requests. For this, we use RewriteCond command.

RewriteCond command

Usage:
RewriteCond [Statement]+[NC]+[OR] RewriteCond process the information like IP Address, site name, etc came from the browsers like Internet Explorer, Mozilla Firefox and Google Chrome.

These are the mostly used statements;
REQUEST_FILENAME: Name of the requested page (ex. index.php)
HTTP_USER_AGENT: Name of the used brwoser or bot(ex. For Firefox Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1 )
QUERY_STRING: Statement after question mark (ex. index.php?page=anasayfa )
SERVER_PROTOCOL: http or https
HTTP_HOST: domain name
THE_REQUEST: Requested page (ex. search.php )
HTTP_REFERER: Site which requested page
[NC] (No Case)
This suffix provides non upper/lowercase sensitive statements.

[OR] (or)
To combine Multi RewriteCond statements.

RewriteRule Statement

You can specify how to write URL after existing condition, with this statement:
[R] (push redirection)
Redirected to external adress.

[F] (forbiddenn)
Redirected to page which indicates no permissions available to this page.

[L] (last rule)
Indicates that this statement is the last statement and no rule run after this point.

Let’s have a look at the examples to sort things up:

Prevent hotlinking

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$ [NC] # if no referrer
RewriteCond %{HTTP_REFERER} !^http://yoursitename.com [NC] # if the referrer is not your site
RewriteCond %{HTTP_REFERER} !^http://www.yoursitename.com [NC] # if the referrer is not your site
RewriteCond %{HTTP_REFERER} !^http://site_ip_address [NC] # if the referrer is not your sites ip
RewriteRule ^.*$ http://www.yoursitename.com/hotlink.jpg [R,L] # redirect to hotlink image

or you can deny the request instead of redirecting to hotlink image:

RewriteRule .*\.(jpe?g|gif|bmp|png)$ - [F]

www redirection

You can use the example below if one or more domain names like domain.com, seconddomain.com redirects to the same site:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.sitename.com$ [NC]
RewriteRule ^(.*)$ http://www.sitename.com/$1 [R,L]

Browser Redirection

It is used when there is differences between browsers while interpreting javascripts and css. Also you can redirect search engine bots such as googlebot to a page which is more plain but no javascript and css at all.By this way you may have a better ranking.
RewriteEngine On

# MS Internet Explorer - Mozilla v4
RewriteCond %{HTTP_USER_AGENT} ^Mozilla/4(.*)MSIE
RewriteRule ^index\.html$ /explorer.html [L]

# Chrome - Mozilla v5 - must be before Firefox rule
RewriteCond %{HTTP_USER_AGENT} ^Mozilla/5(.*)Chrome
RewriteRule ^index\.html$ /chrome.html [L]

# Firefox - Mozilla v5
RewriteCond %{HTTP_USER_AGENT} ^Mozilla/5(.*)Gecko
RewriteRule ^index\.html$ /firefox.html [L]

# Googlebot
RewriteCond %{HTTP_USER_AGENT} ^Googlebot/2(.*)googlebot
RewriteRule ^index\.html$ /google.html [L]

# Lynx or Mozilla v1/2
RewriteCond %{HTTP_USER_AGENT} ^Lynx/ [OR]
RewriteCond %{HTTP_USER_AGENT} ^Mozilla/[12]
RewriteRule ^index\.html$ /resimsiz.html [L]

# others
RewriteRule ^index\.html$ /index.html [L]

Several Home Pages based on Day time

RewriteEngine On
RewriteBase /

# 5:00 > < 8:00 funny code
RewriteCond %{TIME_HOUR} >02
RewriteCond %{TIME_HOUR} <05
RewriteRule ^index\\.html$ /morning.html

# 8:00 > < 16:00
RewriteCond %{TIME_HOUR} >05
RewriteCond %{TIME_HOUR} <13
RewriteRule ^index\\.html$ /day.html

# 16:00 > < 22:00
RewriteCond %{TIME_HOUR} >13
RewriteCond %{TIME_HOUR} <19
RewriteRule ^index\\.html$ /afternoon.html

# 22:00 > < 05:00
RewriteCond %{TIME_HOUR} >19
RewriteCond %{TIME_HOUR} <02
RewriteRule ^index\\.html$ /night.html

HTTP to HTTPS Redirection

In cases where you only want the checkout page to be SSL enabled.

RewriteEngine on
rewritecond %{https} !^on$
rewritecond %{QUERY_STRING} checkout$ [NC]
RewriteRule .* https://sitename.com/checkout.php [R=301,L]

Redirection based on Browser Language

RewriteEngine on

# English
RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteRule ^$ /en/ [L,R=301]

# German
RewriteCond %{HTTP:Accept-Language} ^de [NC]
RewriteRule ^$ /de/ [L,R=301]

# Spain
RewriteCond %{HTTP:Accept-Language} ^es [NC]
RewriteRule ^$ /es/ [L,R=301]

#All other languages
RewriteRule ^$ /default/ [L,R=301]

Third level domain redirection to one site

You might have several domain names pointing the same site like yoursite.com and yoursite.com.au etc and you want to redirect all domain names to yoursite.com, here is the solution:
RewriteEngine On

RewriteCond %{HTTP_HOST} ^(www\.)?yoursite.org [NC,OR]
RewriteCond %{HTTP_HOST} ^(www\.)?yoursite.info [NC,OR]
RewriteCond %{HTTP_HOST} ^(www\.)?yoursite.net [NC,OR]
RewriteCond %{HTTP_HOST} ^(www\.)?yoursite.com.au [NC]
RewriteRule ^(.*)$ http://www.yoursite.com/$1 [R=301,NC, L]
.



		
				
		 
		

	

Leave a Reply

Your email address will not be published. Required fields are marked *