{"id":476,"date":"2012-05-24T14:38:44","date_gmt":"2012-05-24T14:38:44","guid":{"rendered":"http:\/\/www.plugged.in\/?p=476"},"modified":"2012-05-24T14:38:44","modified_gmt":"2012-05-24T14:38:44","slug":"apache-mod_rewrite-url-redirection-with-htaccess","status":"publish","type":"post","link":"https:\/\/www.veriteknik.net.tr\/en\/apache-mod_rewrite-url-redirection-with-htaccess\/","title":{"rendered":"Apache mod_rewrite | URL redirection with .htaccess"},"content":{"rendered":"<p style=\"text-align: justify;\">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: <a title=\"Apache Documentation\" href=\"http:\/\/httpd.apache.org\/docs\/2.2\/mod\/mod_rewrite.html\" target=\"_blank\">Apache Docs<\/a><\/p>\n<p style=\"text-align: justify;\">Usage areas of Apache mod_rewrite are:<\/p>\n<ul>\n<li>URL Redirect Ex: Redirection of plugged.in to www.plugged.in<\/li>\n<li>Browser Redirect Ex: If Internet explorer is below version 7 then redirection to basic folder instead of high technology site<\/li>\n<li>Bot Redirect, Ex: Redirection of Google bot for SEO optimization to a page which contains static links<\/li>\n<li>Redirection to a different sun folder regarding to the browser language<\/li>\n<li>To block out creating hotlinks of picture files Ex: You can prevent consuming bandwidth which is done by pictures used by other sites<\/li>\n<li>SEO SEO Optimizationu, Ex: To show dynamic pages like php, aspx as static like .html<\/li>\n<\/ul>\n<p>used for purposes like above.<\/p>\n<p>In order to use Apache Mod_rewrite, you must have the below instructions in the httpd.conf or in VirtualHost settings:<\/p>\n<pre class=\"brush: bash; gutter: false; first-line: 1\">OPTIONS  FollowSymLinks\n# or\nOPTIONS All\n#and\nAllowOverride All\n#or\nAllowOverride FileInfo<\/pre>\n<p>You must create &#8220;.htaccess&#8221; 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&#8217;t be seen through FTP applications. Because of this you should backup htaccess file locally if you only use ftp to store your files.<br \/>\n.htaccess must begin with this description:<\/p>\n<pre class=\"brush: bash; gutter: false; first-line: 1\"># Apache must know the URLs which we change.\nRewriteEngine On\n# After that line, we will write the URLs to change<\/pre>\n<p>Rewrite works like an algoritm, fist of all we should tell rewrite to change which requests. For this, we use RewriteCond command.<\/p>\n<h4>RewriteCond command<\/h4>\n<p><strong>Usage:<\/strong><br \/>\nRewriteCond [Statement]+[NC]+[OR]\nRewriteCond process the information like IP Address, site name, etc came from the browsers like Internet Explorer, Mozilla Firefox and Google Chrome.<\/p>\n<p>These are the mostly used statements;<br \/>\n<strong>REQUEST_FILENAME:<\/strong> Name of the requested page (ex. index.php)<br \/>\n<strong>HTTP_USER_AGENT:<\/strong> Name of the used brwoser or bot(ex. For Firefox Mozilla\/4.0 (compatible; MSIE 7.0; Windows NT 5.1 )<br \/>\n<strong>QUERY_STRING:<\/strong> Statement after question mark (ex. index.php?page=anasayfa )<br \/>\n<strong>SERVER_PROTOCOL:<\/strong> http or https<br \/>\n<strong>HTTP_HOST:<\/strong> domain name<br \/>\n<strong>THE_REQUEST:<\/strong> Requested page (ex. search.php )<br \/>\n<strong>HTTP_REFERER:<\/strong> Site which requested page<br \/>\n<strong>[NC]<\/strong> (No Case)<br \/>\nThis suffix provides non upper\/lowercase sensitive statements.<\/p>\n<p><strong>[OR]<\/strong> (or)<br \/>\nTo combine Multi RewriteCond statements.<\/p>\n<h4>RewriteRule Statement<\/h4>\n<p>You can specify how to write URL after existing condition, with this statement:<br \/>\n<strong>[R]<\/strong> (push redirection)<br \/>\nRedirected to external adress.<\/p>\n<p><strong>[F]<\/strong> (forbiddenn)<br \/>\nRedirected to page which indicates no permissions available to this page.<\/p>\n<p><strong>[L]<\/strong> (last rule)<br \/>\nIndicates that this statement is the last statement and no rule run after this point.<\/p>\n<p>Let&#8217;s have a look at the examples to sort things up:<\/p>\n<p><strong>Prevent hotlinking<\/strong><\/p>\n<pre class=\"brush: bash; gutter: false; first-line: 1\">RewriteEngine On\nRewriteCond %{HTTP_REFERER} !^$ [NC] # if no referrer\nRewriteCond %{HTTP_REFERER} !^http:\/\/yoursitename.com [NC] # if the referrer is not your site\nRewriteCond %{HTTP_REFERER} !^http:\/\/www.yoursitename.com [NC] # if the referrer is not your site\nRewriteCond %{HTTP_REFERER} !^http:\/\/site_ip_address [NC] # if the referrer is not your sites ip\nRewriteRule ^.*$ http:\/\/www.yoursitename.com\/hotlink.jpg [R,L] # redirect to hotlink image<\/pre>\n<p>or you can deny the request instead of redirecting to hotlink image:<\/p>\n<pre class=\"brush: bash; gutter: false; first-line: 1\">RewriteRule .*\\.(jpe?g|gif|bmp|png)$ - [F]<\/pre>\n<p><strong>www redirection<\/strong><\/p>\n<p><strong><\/strong>You can use the example below if one or more domain names like domain.com, seconddomain.com redirects to the same site:<\/p>\n<pre class=\"brush: bash; gutter: false; first-line: 1\">RewriteEngine On\nRewriteCond %{HTTP_HOST} !^www.sitename.com$ [NC]\nRewriteRule ^(.*)$ http:\/\/www.sitename.com\/$1 [R,L]<\/pre>\n<h4 class=\"brush: bash; gutter: false; first-line: 1\">Browser Redirection<\/h4>\n<pre class=\"brush: bash; gutter: false; first-line: 1\">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.<\/pre>\n<pre class=\"brush: bash; gutter: false; first-line: 1\">RewriteEngine On\n\n# MS Internet Explorer - Mozilla v4\nRewriteCond %{HTTP_USER_AGENT} ^Mozilla\/4(.*)MSIE\nRewriteRule ^index\\.html$ \/explorer.html [L]\n\n# Chrome - Mozilla v5 - must be before Firefox rule\nRewriteCond %{HTTP_USER_AGENT} ^Mozilla\/5(.*)Chrome\nRewriteRule ^index\\.html$ \/chrome.html [L]\n\n# Firefox - Mozilla v5\nRewriteCond %{HTTP_USER_AGENT} ^Mozilla\/5(.*)Gecko\nRewriteRule ^index\\.html$ \/firefox.html [L]\n\n# Googlebot\nRewriteCond %{HTTP_USER_AGENT} ^Googlebot\/2(.*)googlebot\nRewriteRule ^index\\.html$ \/google.html [L]\n\n# Lynx or Mozilla v1\/2\nRewriteCond %{HTTP_USER_AGENT} ^Lynx\/ [OR]\nRewriteCond %{HTTP_USER_AGENT} ^Mozilla\/[12]\nRewriteRule ^index\\.html$ \/resimsiz.html [L]\n\n# others\nRewriteRule ^index\\.html$ \/index.html [L]<\/pre>\n<h4 class=\"brush: bash; gutter: false; first-line: 1\">Several Home Pages based on Day time<\/h4>\n<pre class=\"brush: bash; gutter: false; first-line: 1\">RewriteEngine On\nRewriteBase \/\n\n# 5:00 &gt; &lt; 8:00 funny code\nRewriteCond %{TIME_HOUR} &gt;02\nRewriteCond %{TIME_HOUR} &lt;05\nRewriteRule ^index\\\\.html$ \/morning.html\n\n# 8:00 &gt; &lt; 16:00\nRewriteCond %{TIME_HOUR} &gt;05\nRewriteCond %{TIME_HOUR} &lt;13\nRewriteRule ^index\\\\.html$ \/day.html\n\n# 16:00 &gt; &lt; 22:00\nRewriteCond %{TIME_HOUR} &gt;13\nRewriteCond %{TIME_HOUR} &lt;19\nRewriteRule ^index\\\\.html$ \/afternoon.html\n\n# 22:00 &gt; &lt; 05:00\nRewriteCond %{TIME_HOUR} &gt;19\nRewriteCond %{TIME_HOUR} &lt;02\nRewriteRule ^index\\\\.html$ \/night.html<\/pre>\n<h4 class=\"brush: bash; gutter: false; first-line: 1\">HTTP to HTTPS Redirection<\/h4>\n<p>In cases where you only want the checkout page to be SSL enabled.<\/p>\n<pre class=\"brush: bash; gutter: false; first-line: 1\">RewriteEngine on\nrewritecond %{https} !^on$\nrewritecond %{QUERY_STRING} checkout$ [NC]\nRewriteRule .* https:\/\/sitename.com\/checkout.php [R=301,L]<\/pre>\n<h4 class=\"brush: bash; gutter: false; first-line: 1\">Redirection based on Browser Language<\/h4>\n<pre class=\"brush: bash; gutter: false; first-line: 1\">RewriteEngine on\n\n# English\nRewriteCond %{HTTP:Accept-Language} ^en [NC]\nRewriteRule ^$ \/en\/ [L,R=301]\n\n# German\nRewriteCond %{HTTP:Accept-Language} ^de [NC]\nRewriteRule ^$ \/de\/ [L,R=301]\n\n# Spain\nRewriteCond %{HTTP:Accept-Language} ^es [NC]\nRewriteRule ^$ \/es\/ [L,R=301]\n\n#All other languages\nRewriteRule ^$ \/default\/ [L,R=301]<\/pre>\n<h4 class=\"brush: bash; gutter: false; first-line: 1\">Third level domain redirection to one site<\/h4>\n<pre class=\"brush: bash; gutter: false; first-line: 1\">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:<\/pre>\n<pre class=\"brush: bash; gutter: false; first-line: 1\">RewriteEngine On\n\nRewriteCond %{HTTP_HOST} ^(www\\.)?yoursite.org [NC,OR]\nRewriteCond %{HTTP_HOST} ^(www\\.)?yoursite.info [NC,OR]\nRewriteCond %{HTTP_HOST} ^(www\\.)?yoursite.net [NC,OR]\nRewriteCond %{HTTP_HOST} ^(www\\.)?yoursite.com.au [NC]\nRewriteRule ^(.*)$ http:\/\/www.yoursite.com\/$1 [R=301,NC, L]<\/pre>\n<pre class=\"brush: bash; gutter: false; first-line: 1\">.<\/pre>\n<pre class=\"brush: bash; gutter: false; first-line: 1\"><\/pre>\n<pre class=\"brush: bash; gutter: false; first-line: 1\"><\/pre>\n<pre class=\"brush: bash; gutter: false; first-line: 1\"><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","footnotes":""},"categories":[367],"tags":[365,525,526,527,528],"yst_prominent_words":[],"class_list":["post-476","post","type-post","status-publish","format-standard","hentry","category-httpd","tag-apache","tag-htaccess","tag-mod_rewrite","tag-redirection","tag-url"],"jetpack_featured_media_url":"","uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"ckaraca","author_link":"https:\/\/www.veriteknik.net.tr\/en\/author\/ckaraca\/"},"uagb_comment_info":0,"uagb_excerpt":"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&hellip;","_links":{"self":[{"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/posts\/476","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/comments?post=476"}],"version-history":[{"count":0,"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/posts\/476\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/media?parent=476"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/categories?post=476"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/tags?post=476"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/www.veriteknik.net.tr\/en\/wp-json\/wp\/v2\/yst_prominent_words?post=476"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}