httpd-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Nigel Peck <np.li...@sharphosting.uk>
Subject [users@httpd] mod_rewrite - Setting Environment Variables
Date Sat, 04 Feb 2017 07:03:41 GMT

Hi,

I am having trouble with how mod_rewrite handles environment variables 
that are set with the [E=VAR:VAL] flag for RewriteRule. I am setting an 
environment variable to prevent a loop. The logs show it is set, and 
then matched, but then on the subsequent iteration of the rules it is no 
longer set (so the loop occurs that it is supposed to prevent).

The directives I am testing with are:

RewriteRule ^((?!pages/).*)$ pages/$1 [env=rewritten:1]

RewriteCond %{ENV:rewritten} !=1
RewriteRule ^pages(/.*)$ $1 [R=301,L]

One cycle of the log output is below. Line 8 clearly shows the 
environment variable is there and prevents the second rule running. But 
then on line 15, on the next iteration, it has disappeared again and 
returns nothing. What is going on? Am I missing something? Surely an 
environment variable should not disappear again on the next iteration.

1  strip per-dir prefix: /eg/trent -> trent
2  applying pattern '^((?!pages/).*)$' to uri 'trent'
3  rewrite 'trent' -> 'pages/trent'
4  setting env variable 'rewritten' to '1'
5  add per-dir prefix: pages/trent -> /eg/pages/trent
6  strip per-dir prefix: /eg/pages/trent -> pages/trent
7  applying pattern '^pages(/.*)$' to uri 'pages/trent'
8  RewriteCond: input='1' pattern='!=1' => not-matched
9  strip document_root prefix: /eg/pages/trent -> /pages/trent
10 internal redirect with /pages/trent [INTERNAL REDIRECT]
11 strip per-dir prefix: /eg/pages/trent -> pages/trent
12 applying pattern '^((?!pages/).*)$' to uri 'pages/trent'
13 strip per-dir prefix: /eg/pages/trent -> pages/trent
14 applying pattern '^pages(/.*)$' to uri 'pages/trent'
15 RewriteCond: input='' pattern='!=1' => matched
16 rewrite 'pages/trent' -> '/trent'
17 explicitly forcing redirect with http://example.com/trent
18 escaping http://example.com/trent for redirect
19 redirect to http://example.com/trent [REDIRECT/301]

If anyone could shed some light on this it would be much appreciated. I 
know I could achieve my objective in other ways. I'm not looking for 
help with that. I'm looking to understand how environment variables work 
with mod_rewrite and why the above is happening.

This is with Apache 2.4.6.

Thanks
Nigel

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Mime
View raw message