| 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 :
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. Rewrite works like an algoritm, fist of all we should tell rewrite to change which requests. For this, we use RewriteCond command. RewriteCond commandUsage: 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) [NC] (No Case)
RewriteRule StatementYou can specify how to write URL after existing condition, with this statement: [R] (push redirection)
Lets see all the examples together: Blocking the reuqest of picture files out of the siteRewriteEngine 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 redirectionU 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 BrowserIt 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 timeRewriteEngine 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önlendirmeRewriteEngine on
rewritecond %{https} !^on$ rewritecond %{QUERY_STRING} siparis$ [NC] RewriteRule .* https://alanadiniz.com/siparis [R=301,L]
Redirecting based on Browser LanguageRewriteEngine 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 nameIF 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] |