Search code examples
.htaccessmodx

How do I add to my URL from .htaccess based on cookie


I have a site that is creating a cookie based on a users geoIP, so that the location is always part of the URL, like example.com/dc-metro/fly-a-kite. I am having an issue when a user comes to example.com/fly-a-kite and inserting the correct location to the URL. Here is what I have in my .htaccess file:

RewriteCond %{HTTP_COOKIE} market=([dc-metro]) [NC]
RewriteRule ^fly-a-kite(.*)$ /dc-metro/fly-a-kite$1 [L,R=301]

RewriteCond %{HTTP_COOKIE} market=([chicago]) [NC]
RewriteRule ^fly-a-kite(.*)$ /chicago/fly-a-kite$1 [L,R=301]

I am using MODX, which I don't think should make a difference, the problem I am having is that when I try going directly to example.com/fly-a-kite it is inserting different locations other than what should be my saved cookie.

Thank you for any help you can provide.


Solution

  • I found a solution last night that is working, not sure if it is the best answer, but it is working for me. Instead of using ([name]) for the cookie value I got rid of the ([ ]) so my htaccess looks like:

    RewriteCond %{HTTP_COOKIE} market=dc-metro [NC]
    RewriteRule ^fly-a-kite(.*)$ /dc-metro/fly-a-kite$1 [L,R=301]
    
    RewriteCond %{HTTP_COOKIE} market=chicago [NC]
    RewriteRule ^fly-a-kite(.*)$ /chicago/fly-a-kite$1 [L,R=301]