user1032531
user1032531

Reputation: 26281

What does % (percent sign) do in Apache's httpd.conf?

Okay, I see the following example from http://httpd.apache.org/docs/2.0/misc/rewriteguide.html. Does %{ and } makes Apache interpret the string between these two deliminators as an Apache variable? Where is this functionality documented?

RewriteCond %{HTTP_HOST}   !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteCond %{SERVER_PORT} !^80$
RewriteRule ^/?(.*)         http://www.example.com:%{SERVER_PORT}/$1 [L,R,NE]

Then, http://httpd.apache.org/docs/2.2/configuring.html describes using shell variables as ${ENVVAR}. What is the difference?

Upvotes: 6

Views: 2757

Answers (1)

MrWhite
MrWhite

Reputation: 45829

The %{NAME_OF_VARIABLE} syntax (not simply the % symbol) is simply mod_rewrite's mechanism for accessing one of a predefined list of server variables (specific to mod_rewrite). As documented for the RewriteCond directive.

Without the enclosing %{..} then NAME_OF_VARIABLE is seen as a literal string.

To access environment variables in mod_rewrite you use the syntax: %{ENV:variable}. (The other syntax ${ENVVAR} also appears to work here, however, there is a difference in behaviour if ENVVAR does not exist. If ENVAR does not exist then ${ENVVAR} returns the literal string "${ENVAR}", whereas %{ENV:ENVAR} returns an empty string. There might also be a conflict if you are using rewrite maps in RewriteCond since a similar syntax is used. ie ${mapname:key|default} - although I would think the : should make this unambiguous?)

Upvotes: 5

Related Questions