Home
Apache mod_rewrite | URL redirection with .htaccess

Apache re_write 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 company here:go.

Usage areas of Apache mod_rewrite :

  • URL Redirect Ex: Redirection of veriportal.com'u to veriportal.
  • 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 VirtualHost settings:

OPTIONS  FollowSymLinks
# ya da
OPTIONS All
#ve
AllowOverride All
#ya da
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 similar systems, this is why htaccess cant be seen in FTP applications. Because of this you should backup htaccess file.

.htaccessmust 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 informations those come from apache in touch browsers like Internet Explorer, Mozilla Firefox and Google Chrome, from IP adresses or redirected sites, 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 add provides non upper/lowercase sensitive searched statement.

[OR] (veya)
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] (İzin verilmeyen)
Redirected to page which indicates no log in permission available to this page.


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

 

Lets see all the examples together:

Blocking the reuqest of picture files out of the site

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$ [NC]
RewriteCond %{HTTP_REFERER} !^http://alanadiniz.com [NC]
RewriteCond %{HTTP_REFERER} !^http://www.alanadini.com [NC]
RewriteCond %{HTTP_REFERER} !^http://192.168.1.10 [NC]
RewriteRule ^.*$ http://www.alanadiniz.com/hotlink.jpg [R,L]

 

Alternative:

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?alanadiniz\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule .*\.(jpe?g|gif|bmp|png)$ http://www.alanadiniz.com/hotlink.jpg [L]

Also you can restrict instead of redirect to another picture with [F]

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

 

www redirection

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

 

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

 

Redirection regarding to Browser

It is used when there is differences between browsers while interpreting javascripts and css. Also you can redirect searchengines bots like googlebot to a page which is more plain but  lacks of javascriopts and css. By this way, you can reduce the possibility of search engines mistakes and you may have a better rating place.

RewriteEngine On

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

# Chrome - Mozilla v5 - Firefox kuralindan once olmalidir
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 ya da Mozilla v1/2
RewriteCond %{HTTP_USER_AGENT} ^Lynx/ [OR]
RewriteCond %{HTTP_USER_AGENT} ^Mozilla/[12]
RewriteRule ^index\.html$ /resimsiz.html [L]

# Diğerleri
RewriteRule ^index\.html$ /index.html [L]

 

Different start Pages based on Day time

RewriteEngine On
RewriteBase /

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

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

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

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

 

HTTP sayfayı HTTPS'e yönlendirme

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

 

Redirecting based on Browser Language

RewriteEngine on

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

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

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

#Diger butun diller ve Varsayilan dil Turkce
RewriteRule ^$ /tr/ [L,R=301]

 

 Combinig  a couple of names into one domain name

IF you have lots of names belong to your site like veriportal.com, veriportal.net, veriportal.com.tr and you want to open these pages from just one site ( you should otherwise your rank will be seperated ) then follow the steps below:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(www\.)?veriportal.org [NC,OR]
RewriteCond %{HTTP_HOST} ^(www\.)?veriportal.info [NC,OR]
RewriteCond %{HTTP_HOST} ^(www\.)?veriportal.net [NC,OR]
RewriteCond %{HTTP_HOST} ^(www\.)?veriportal.com.tr [NC]
RewriteRule ^(.*)$ http://www.veriportal.com/$1 [R=301,NC, L]
 
VeriTeknik I.T. Solutions Ltd., is a DataCenter company in Turkey.
VeriTeknik Telecom Ltd., is an Internet Service Provider in Turkey.

VeriTeknik I.T. Sloutions Ltd., VeriTeknik Telecom Ltd. 2004 - 2010
Eskişehir Yolu 17.km. Çamlık Park Sitesi 2365. sk. No:7 Bağlıca Ankara Turkey +90.312 234 20 00