Home > Software design >  Redirect same page but add query string by .htaccess if cookie value
Redirect same page but add query string by .htaccess if cookie value

Time:10-03

I have a cookie created to identify the city.

The name of the cookie is this: geodefense that contains the value of the city Piura (This value can change from the cities of my country according to customer)

I need to redirect by adding some query string by .htaccess to prevent the page from loading showing prices that are not for the city

For example if I am on the page

https://127.0.0.1/myproduct1

Redirect only if the geodefense cookie exists and take the value for the query string

https://127.0.0.1/myproduct1?geo=[cookievalue]

Currently I have this .htaccess code to check if a cookie exists but not how to take the value of the cookie and redirect to the same page but adding the query string

RewriteEngine On
RewriteCond %{HTTP_COOKIE} !cookie_name=specific_value; [NC]
RewriteRule ^ http://www.google.com [NC,L]

CodePudding user response:

Currently I have this .htaccess code to check if a cookie exists ...

RewriteCond %{HTTP_COOKIE} !cookie_name=specific_value; [NC]
RewriteRule ^ http://www.google.com [NC,L]

That code checks that the cookie specific value does not exist (as denoted by the ! prefix on the CondPattern).

However, this would seem to be a bit of an anti-pattern... unnecessarily redirecting the user when the value is already present (and available to your script) in the cookie?! Anyway, to answer your specific question, you would need to do something like this:

RewriteCond %{QUERY_STRING} !(^|&)geo=
RewriteCond %{HTTP_COOKIE} (?:^|\s|;)geodefense=(\w )($|\s|;)
RewriteRule ^ %{REQUEST_URI}?geo=%1 [QSA,R=302,L]

The above redirects any URL that does not already have a geo URL parameter and the geodefense cookie exists with a non-empty value to the same URL, appending the value of the geodefense cookie as the value of the geo URL-parameter. Any other query string parameters on the original request are then appended (the result of the QSA flag).

The first condition (RewriteCond directive) checks that the geo URL parameter does not already exist on the URL. This is necessary to prevent a redirect loop, but also means that the URL parameter will take priority over the cookie value, if both are present. This is probably a non-issue and may even be desirable, however, you could potentially validate that the URL parameter (if present) matches the cookie value and replace it if it's different - at the expense of additional complexity.

The second condition checks for the geodefense cookie and captures its value using the parenthesised subpattern (\w ). This is then available in the RewriteRule substitution string using the %1 backreference. \w is a shorthand character class that denotes word characters and is equivalent to the character class [a-zA-Z0-9_].


UPDATE:

is possible to disable this behavior for example on the cart page that would be like this 127.0.0.1/store/cart

You can add an exception - a negated condition (RewriteCond directive) - before the existing conditions. For example:

RewriteCond %{REQUEST_URI} !=/store/cart
:

The above checks that the requested URL-path is not exactly equal to /store/cart. The = prefix operator makes this an exact match string comparison, rather than a regex. Use a regex if you need more flexibility, for example:

RewriteCond %{REQUEST_URI} !^/store/
:

The above (regex) checks that the URL-path does not start with /store/. So naturally excludes /store/cart and /store/<anything>.

The order of these rules in your .htaccess file is important. This rule must go before any front-controller pattern you might have, near the top of the .htaccess file (but after any blocking directives).

  • Related