htaccess with Cookies


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

htaccess with Cookies

  1. 1. Htaccess - Complex redirections Cristina Martin, Web developer [email_address] Epoint Web Solutions
  2. 2. Table of Content <ul><li>Customer requirements </li></ul><ul><li>Problem analysis and solutions </li></ul><ul><ul><li>GeoIP </li></ul></ul><ul><ul><li>Cookies </li></ul></ul><ul><ul><li>.htaccess </li></ul></ul><ul><li>Proxy websites </li></ul><ul><li>Links/References </li></ul>
  3. 3. Customer requirements <ul><li>Content sections of the website different for the available countries </li></ul><ul><li>An user from Germany will enter in the German section of the website with content in German language </li></ul><ul><li>If the user from Germany prefer the content from the UK section and Polski language, on his return he will enter directly on the UK section of the website with content in Polski </li></ul><ul><li>Multilingual website </li></ul>
  4. 4. Solutions: GeoIP <ul><li>Geographic Internet Protocol address location </li></ul><ul><li>GeolP Country - regularly updated database with information about IP addresses issued for those countries </li></ul><ul><li>$country_code = apache_note(”GEOIP_COUNTRY_CODE”); </li></ul><ul><li>$country_name = apache_note(GEOIP_COUNTRY_NAME); </li></ul>
  5. 5. Solutions: Cookies <ul><li>Used to retain the user preferences for country and language. </li></ul><ul><li>In case the user will return several times, should not be forced to make the settings again and again. </li></ul><ul><li>setcookie (&quot;Country&quot;, $ country, 0,'/'); </li></ul><ul><li>setcookie (&quot;Language&quot;, $ language, 0,'/'); </li></ul>
  6. 6. Solutions: .htaccess <ul><li>Can be used to rewrite URLs </li></ul><ul><li>First case scenario: the visitor is automatically redirected in the related country's section, checked through the IP address of the user with content in the default country's language </li></ul><ul><li>Second case scenario: the visitor is automatically redirected in the preffered country's section with content in the language choosen by him through cookies </li></ul>
  7. 7. .htaccess - First case <ul><li>RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^RO$ </li></ul><ul><li>RewriteCond %{REQUEST_URI} !^/index.php$ </li></ul><ul><li>RewriteCond %{HTTP_COOKIE} !^.*Language.*$ [NC] </li></ul><ul><li>RewriteCond %{HTTP_COOKIE} !^.*Country.*$ [NC] </li></ul><ul><li>RewriteRule ^(.*)$ /index.php?id=81&L=2 [L] </li></ul><ul><li>[NC] (no-case) - This makes the test case-insensitive - differences between 'A-Z' and 'a-z' are ignored when is match with the current URL. </li></ul><ul><li>[L] (last rule) - Stop the rewriting process here and don't apply any more rewrite rules </li></ul>
  8. 8. .htaccess – First case <ul><li>First condition : If the user is in Romania; </li></ul><ul><li>The second condition : If the user has accessed / index.php; </li></ul><ul><li>The third condition : If the cookie Language isn't set; </li></ul><ul><li>The fourth condition : If the cookie Country isn't set; </li></ul><ul><li>Rewrite rule : If all the conditions are met, the user is redirected to that URL </li></ul>
  9. 9. .htaccess – Second case <ul><li>RewriteCond %{REQUEST_URI} !^/index.php$ </li></ul><ul><li>RewriteCond %{HTTP_COOKIE} Language=([^;]+) [NC] </li></ul><ul><li>RewriteRule ^(.*)$ /index.php?L=%1 [NC,C] </li></ul><ul><li>RewriteCond %{HTTP_COOKIE} Country=([^;]+) [NC] </li></ul><ul><li>RewriteRule ^(.*)$ /index.php?id=%1%2 [NC,L,QSA] </li></ul><ul><li>[C] (chained with next rule) - Chains the current rule with the next rule. If a rule matches, then the rule is processed further, but if the rule does not match, then all the following chained rule will not be processed </li></ul><ul><li>[QSA] (query string append) - Forces the rewriting engine to add a query string part of the existing string, instead of replacing it. </li></ul>
  10. 10. .htaccess – Second case <ul><li>First condition : If the user has accessed / index.php; </li></ul><ul><li>The second condition : take the value of the cookie Language </li></ul><ul><li>Rewrite rule : rewrite based on our cookie value </li></ul><ul><li>The third condition : take the value of the cookie Country </li></ul><ul><li>Rewrite rule : rewrite based on the values of our cookies Country and Language </li></ul>
  11. 11. Proxy websites <ul><li>Check at the following links: </li></ul><ul><ul><li>With an IP in Poland: </li></ul></ul><ul><ul><li>With an IP Germany: </li></ul></ul><ul><ul><li>With a USA IP: </li></ul></ul><ul><li>There are many proxy websites (google-it) </li></ul>
  12. 12. Links/References <ul><li> </li></ul><ul><li> </li></ul><ul><li> </li></ul><ul><li> </li></ul><ul><li> </li></ul>