Reputation: 11
I'm using Apache 2.2 and want to add a string to the request URI if it is not already part of the URI. This string is dynamic and stored in a cookie called "mycookie". If the cookie value is "root" no rewrite should be performed.
Case 1: Rewrite required
mycookie=foo
uri=/bar -> uri=/foo/bar (/foo added)
Case 2: No rewrite required
mycookie=foo
uri=/foo/bar -> no rewrite (URI already starts with /foo)
Case 3: No rewrite, cookie value ignored
mycookie=root
uri=/bar -> no rewrite (value "root" is ignored)
#no redirect for cookie value „root“
RewriteCond %{HTTP_COOKIE} ^.*mycookie=([a-zA-Z0-9-\.]+).* [NC]
RewriteCond %1 =root
RewriteRule ^(.*)$ - [L,NC]
#no rewrite required if cookie value is already part of the request uri
RewriteCond %{HTTP_COOKIE} ^.* mycookie=([a-zA-Z0-9-\.]+).* [NC]
# Wrong syntax
# RewriteCond %{REQUEST_URI} ^.*/%1/.*$ [NC]
# Correct syntax
RewriteCond %1::%{REQUEST_URI} ^(.*?)::/\1/?
RewriteRule ^(.*)$ - [L,NC]
#add cookie value to path: eg. /bar -> /cookieval/bar
RewriteCond %{HTTP_COOKIE} ^.* mycookie =([a-zA-Z0-9-\.]+).* [NC]
RewriteRule ^(.*)$ https://sub.domain.com/%1$1 [R,L]
The problem is that requests are rewriten within a loop. -> /foo/foo/foo/../foo/bar
Solution Backreferences (%1) can only be used on the left side of a RewriteCond
Upvotes: 0
Views: 2117
Reputation: 11
Backreferences (%1) can only be used on the left side of a RewriteCond
Changed RewriteCond %{REQUEST_URI} ^./%1/.$ [NC]
To RewriteCond %1::%{REQUEST_URI} ^(.*?)::/\1/?
Upvotes: 1