Forum Moderators: phranque
# an exact copy and paste, except that I changed the regex matches for brevity
<FilesMatch "\.(php|cgi|lib)$">
...
<If "%{ENV:linker} =~ /^lorem(?:ipsum)?$/ && %{REQUEST_URI} !~ /^\/(?:foo|bar)[\/$]/i">
<If "%{QUERY_STRING} =~ /(?:^|&)default=([A-Za-z]+)/">
RewriteRule ^ - [E=default:${lc:%1}]
</If>
<Else>
RewriteRule ^/([a-z]+) - [E=default:${lc:$1}]
</Else>
RewriteRule ^/([a-z]+) - '[E=foo:bar]'
</If>
...
</FilesMatch> <If> directive missing closing '>' # www.example.com sets %{ENV:linker} = example
RewriteCond %{HTTP_HOST} ^(?:www\.)([^.]+)\. [NC]
RewriteRule ^ - [E=linker:${lc:%1}]
# expecting %{ENV:tester} = ex
<If "%{ENV:linker} =~ /(ex)ample/i">
RewriteRule ^ - [E=tester:%1]
</If> RewriteCond %{HTTP_HOST} ^(?:www\.)([^.]+)\. [NC]
RewriteRule ^ - [E=linker:${lc:%1}]
<If "%{HTTP_HOST} =~ m#^(?:www\.)(ex)ample\.#i">
RewriteRule ^ - [E=tester:%1]
</If> RewriteRule ^ - [E=foo:bar]
<FilesMatch "\.php$">
<If "-n %{ENV:foo}">
RewriteRule ^ - [E=lorem:ipsum]
</If>
RewriteRule ^ - [E=this:that]
</FilesMatch> RewriteRule ^ - [E=foo:bar]
<If "-n %{ENV:foo}">
RewriteRule ^ - [E=lorem:ipsum]
</If>
RewriteRule ^ - [E=this:that] SetEnvIfExpr "%{HTTP_HOST} =~ m#^(?:www\.)?(\w+)\.#i" foo=bar
<If "-n %{ENV:foo}">
RewriteRule ^ - [E=lorem:ipsum]
</If>
RewriteRule ^ - [E=this:that] Apache says somewhere that using mod_rewrite inside <Files> envelopes will work--probably--but it isn't supported and you do so at your own risk.
^rats/ This technique is offered as an example, not as a recommendation.
<If>, <ElseIf>, and <Else> are applied last.except that, in the fine tradition of Samuel Johnson's garret and cockloft,
Certain variables, such as CONTENT_TYPE and other response headers, are set after <If> conditions have already been evaluated
Certain variables, such as CONTENT_TYPE and other response headers, are set after <If> conditions have already been evaluated
RewriteRule ^ - [E=foo:bar]
SetEnvIfExpr "-n %{ENV:foo}" lorem=ipsum
RewriteRule ^ - [E=this:that] SetEnvIf Request_URI "\.php$" foo=bar
SetEnvIfExpr "-n %{ENV:foo}" lorem=ipsum blah=blerg
RewriteRule ^ - [E=this:that] SetEnvIf Request_URI "\.php$" foo=bar
SetEnvIf foo [a-z] lorem=ipsum blah=blerg
RewriteRule ^ - [E=this:that] [edited by: csdude55 at 6:15 am (utc) on Jan 25, 2023]
it sets foo and this but not loremwell, that makes sense, since mod_setenvif executes before mod_rewrite. So at the time of the SetEnvIfExpr directive, foo has not yet been set. Remember that each module is an island; it makes no difference whether a RewriteRule is physically located above or below a SetEnvIf.
I was able to apply multiple variables by delimiting with a spaceYup, that's in the docs:
https://httpd.apache.org/docs/2.4/mod/mod_setenvif.html#setenvifAny extra stuff after the “attribute” and “regex” elements will turn into as many environmental variables as you want. I was going to say that I've never personally tried it--but, come to think of it, I do it all the time when UNsetting environmental variables, as in BrowserMatch Googlebot !noaccept !nolang !en_only !lying_bot
Thanks to Apache's ultra-minimalist grammar, where spaces have syntactic meaning of one kind, and line breaks have syntactic meaning of a different kind, you're pretty well stuck with long lines.
In short: variables set in mod_setenvif will be seen by mod_rewrite, but not the other way around ... unless the directives that need to execute later are inside a <Files> envelope. Which, I guess, is the argument in favor of <Files>.
RewriteRule ^ - [E=foo:bar]
<FilesMatch "\.php$">
SetEnvIf foo [a-z] blah=blerg
<If "-n %{ENV:foo}">
RewriteRule ^ - [E=lorem:ipsum]
</If>
</FilesMatch> It's been a while, but I think you have to say
RewriteEngine on
over again inside any Files envelope, the same way you do in anything directory-specific.
RewriteRule ^ - [E=foo:bar]
<FilesMatch "\.php$">
RewriteEngine on
SetEnvIf foo [a-z] blah=blerg
<If "-n %{ENV:foo}">
RewriteRule ^ - [E=lorem:ipsum]
</If>
</FilesMatch>
RewriteRule ^ - [E=this:that] # httpd -M
Loaded Modules:
core_module (static)
so_module (static)
http_module (static)
mpm_event_module (shared)
systemd_module (shared)
cgid_module (shared)
access_compat_module (shared)
actions_module (shared)
alias_module (shared)
auth_basic_module (shared)
authn_core_module (shared)
authn_file_module (shared)
authz_core_module (shared)
authz_groupfile_module (shared)
authz_host_module (shared)
authz_user_module (shared)
autoindex_module (shared)
deflate_module (shared)
dir_module (shared)
expires_module (shared)
filter_module (shared)
headers_module (shared)
include_module (shared)
log_config_module (shared)
logio_module (shared)
mime_module (shared)
negotiation_module (shared)
proxy_module (shared)
proxy_fcgi_module (shared)
proxy_http_module (shared)
proxy_wstunnel_module (shared)
rewrite_module (shared)
setenvif_module (shared)
slotmem_shm_module (shared)
socache_dbm_module (shared)
socache_shmcb_module (shared)
speling_module (shared)
status_module (shared)
unique_id_module (shared)
unixd_module (shared)
userdir_module (shared)
version_module (shared)
bwlimited_module (shared)
ssl_module (shared)
fcgid_module (shared)
http2_module (shared)
pagespeed_module (shared)
security2_module (shared)