Check cookie and redirect with Apache

14,718

Solution 1

  1. To configure Apache to do this, use the following, replacing with the cookie name.

    RewriteEngine on
    RewriteCond %{REQUEST_URI} ^/reviews/?$
    RewriteCond %{HTTP_COOKIE} <cookie>=([^;]+)
    RewriteRule .* http://mydomain.com/reviews/%1 [R=302,L]
    
  2. Yelp stores the location in a cookie, so I'd take that as a good sign, since they have a ton of traffic and appear to be doing well.

There are pros and cons to using Apache to do the redirect, but the main con, is that it is easier to maintain the rewrite rule in your code instead of on the server. You can quickly make fixes and deploy, instead of having to change and restart all Apache servers.

Solution 2

There is also a way to give a custom default value if no cookie is set directly in the rewrite rules!

The %1 always refers to the last RewriteCond evaluated! If a cookie is found, the second RewriteCond is ignored. If none is found, the second one is evaluated. All it does is to give a value and then match this whole value!

RewriteEngine on
RewriteCond %{HTTP_COOKIE} cookiename=([^;]+) [OR]
RewriteCond defaultvalue (.*)
RewriteRule ^(.*)$ /mypath/%1 [L,R=302]

You can add other RewriteCond checks, but this have to be the last two lines before the final RewriteRule

Share:
14,718
Per Sandström
Author by

Per Sandström

Since 1999, I’ve been designing, developing, and leading web projects for organizations and non-profits, from large to small. I have a chronic thirst for knowledge and approach things through thinking, challenging, and problem-solving. I love building things and thrive in creative and prestigeless environments, where creating value is what matters.

Updated on June 06, 2022

Comments

  • Per Sandström
    Per Sandström almost 2 years

    I'd love to get some feedback on this. I'm not sure if it's the right approach.

    The details

    I'm running Apache 2 with PHP 5.3/MySQL 4 and Drupal 6 is the platform.

    I'm developing a site which contains restaurant reviews in a couple of selected cities. When the users arrives at the site it can choose which city is theirs. I store their choice in a cookie and if they haven't made a choice I've selected a default city.

    Proposed solution

    Now I want the URL mydomain.com/reviews to redirect to the city specific URL based on their city choice. For example mydomain.com/reviews/paris if I've selected Paris as my city. (If there's no cookie set it should redirect to the default city.)

    I consider this the best alternative because I want the user to be able to see reviews in another city without changing their city. If they'd like to view reviews for London restaurant they can simply go to mydomain.com/reviews/london.

    For the best performance I'm thinking of having Apache check the cookie and make the redirect to the right city when the user goes to mydomain.com/reviews.

    So here are my questions…

    1. How do I configure Apache to do this?
    2. Is this the best way to go?
  • Per Sandström
    Per Sandström almost 13 years
    Thanks! Can I set the default cookie (say "paris") in Apache if the user hasn't got one?
  • toneplex
    toneplex almost 13 years
    You could use Set-Cookie which is documented here httpd.apache.org/docs/2.3/mod/mod_headers.html, but if I were you, I would do it in your code.
  • DrCord
    DrCord over 10 years
    Replacing <cookie> with the name of the cookie you would like to get the value of. Also something that tripped me up on this was the %1 backreference only works to the last RewriteCond, I had a rule in between and couldn't get it to work right.