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?
The main configuration file is usually called httpd. conf . The location of this file is set at compile-time, but may be overridden with the -f command line flag. In addition, other configuration files may be added using the Include directive, and wildcards can be used to include many configuration files.
The Listen command identifies the ports on which the Web server accepts incoming requests. By default, the Apache HTTP Server is set to listen to port 80 for non-secure Web communications and (in the /etc/httpd/conf.
ServerName. ServerName specifies a hostname and port number (matching the Listen directive) for the server. The ServerName does not need to match the machine's actual hostname.
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?)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With