Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 935
user1032531 Avatar asked Feb 03 '15 02:02

user1032531


People also ask

What is in httpd CONf?

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.

Which directive controls the HTTP port for the Apache Web server?

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.

Which configuration option of Apache is used to define the hostname and port that the server uses to identify itself?

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.


1 Answers

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?)

like image 123
MrWhite Avatar answered Sep 28 '22 15:09

MrWhite