{"id":104,"date":"2013-08-29T11:31:28","date_gmt":"2013-08-29T09:31:28","guid":{"rendered":"http:\/\/www.miczanin.it\/mz_en\/?p=104"},"modified":"2013-08-29T11:33:01","modified_gmt":"2013-08-29T09:33:01","slug":"redirect","status":"publish","type":"post","link":"https:\/\/www.miczanin.it\/mz_en\/2013\/08\/29\/redirect\/","title":{"rendered":"mod_rewrite and .htaccess: redirect based on browser language"},"content":{"rendered":"<p style=\"text-align: justify;\">The <a title=\"Renovation\" href=\"https:\/\/www.miczanin.it\/mz_en\/2013\/08\/18\/renovation\/\">previous version<\/a> of miczanin.it had just an <em>index.html<\/em> file in the root folder of the web server, together with few other supporting html files. When someone tried to access <em>http:\/\/www.miczanin.it<\/em>, the <em>index.html<\/em> page was automatically loaded.<\/p>\n<p style=\"text-align: justify;\">The current configuration is different. There are two distinct WordPress installations, one in subfolder <em>mz_it<\/em> (in Italian), one in subfolder <em>mz_en<\/em> (in English). My goal is to automatically redirect <em>http:\/\/www.miczanin.it<\/em> to <em>mz_it<\/em> or <em>mz_en<\/em>, according to browser language preferences.<\/p>\n<p style=\"text-align: justify;\">There are many possible solutions, but I think the cleanest one is to use a server-side approach based on <em>.htaccess<\/em> file and <em>mod_rewrite<\/em> module. You can find some documentation, a tutorial and a cheat sheet starting from this page: <a href=\"http:\/\/www.addedbytes.com\/articles\/for-beginners\/url-rewriting-for-beginners\/\" target=\"_blank\">URL Rewriting for Beginners<\/a>.<\/p>\n<p style=\"text-align: justify;\">This is the <em>.htaccess<\/em> file in the root folder of <em>miczanin.it<\/em> web server:<\/p>\n<pre>&lt;IfModule mod_rewrite.c&gt;\r\n# Turn on the rewriting engine\r\n<strong>RewriteEngine<\/strong> On \r\n<strong>RewriteBase<\/strong> \/\r\n\r\n# If Accept-Language starts with 'it',\r\n# then redirect (only) main page to mz_it\r\n<strong>RewriteCond<\/strong> %{HTTP:Accept-Language} ^it [NC]\r\n<strong>RewriteRule<\/strong> ^\/?$ mz_it\/ [R,NC,L]\r\n\r\n# Otherwise, redirect to mz_en\r\n<strong>RewriteRule<\/strong> ^\/?$ mz_en\/ [R,NC,L]\r\n&lt;\/IfModule&gt;<\/pre>\n<p style=\"text-align: justify;\">The idea is to exploit the optional <em>Accept-Language<\/em> information that browsers can embed in their HTTP requests. According to the <a title=\"HTTP Accept-Language documentation\" href=\"http:\/\/www.w3.org\/Protocols\/rfc2616\/rfc2616-sec14.html#sec14.4\" target=\"_blank\">official documentation<\/a>, it is a comma separated list of language definitions. It could be straightforward like &#8216;<em>it<\/em>&#8216;, or &#8216;<em>en<\/em>&#8216;, but something more complicated like &#8216;<em>en-gb;q=0.7<\/em>&#8216; is also a valid declaration.<\/p>\n<p style=\"text-align: justify;\">If the language list starts with &#8216;<em>it<\/em>&#8216;, then <em>http:\/\/www.miczanin.it<\/em> is redirected to <em>mz_it<\/em>. If no language preferences are defined, or if Italian is not the first choice, then the redirection goes to <em>mz_en<\/em>.<\/p>\n<p style=\"text-align: justify;\">In case &#8216;<em>it<\/em>&#8216; appears before &#8216;<em>en<\/em>&#8216;, but not in first place (example &#8216;<em>fr, it, en<\/em>&#8216;), then the <em>.htaccess<\/em> rules still redirect to <em>mz_en<\/em>. This behaviour is not desirable, but for the moment I consider the described solution acceptable. I wonder if it is possible to devise a regular expression for <em>RewriteCond<\/em> that looks for &#8216;<em>it<\/em>&#8216; appearing before any of the &#8216;<em>en-*<\/em>&#8216; or &#8216;<em>en<\/em>&#8216;&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The previous version of miczanin.it had just an index.html file in the root folder of the web server, together with few other supporting html files. When someone tried to access http:\/\/www.miczanin.it, the index.html page was automatically loaded. The current configuration &hellip; <a href=\"https:\/\/www.miczanin.it\/mz_en\/2013\/08\/29\/redirect\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[4],"class_list":["post-104","post","type-post","status-publish","format-standard","hentry","category-post","tag-webdev"],"_links":{"self":[{"href":"https:\/\/www.miczanin.it\/mz_en\/wp-json\/wp\/v2\/posts\/104","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.miczanin.it\/mz_en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.miczanin.it\/mz_en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.miczanin.it\/mz_en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.miczanin.it\/mz_en\/wp-json\/wp\/v2\/comments?post=104"}],"version-history":[{"count":10,"href":"https:\/\/www.miczanin.it\/mz_en\/wp-json\/wp\/v2\/posts\/104\/revisions"}],"predecessor-version":[{"id":114,"href":"https:\/\/www.miczanin.it\/mz_en\/wp-json\/wp\/v2\/posts\/104\/revisions\/114"}],"wp:attachment":[{"href":"https:\/\/www.miczanin.it\/mz_en\/wp-json\/wp\/v2\/media?parent=104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.miczanin.it\/mz_en\/wp-json\/wp\/v2\/categories?post=104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.miczanin.it\/mz_en\/wp-json\/wp\/v2\/tags?post=104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}