Apache mod_rewrite: explain me %{HTTP_HOST} expecially when using addon domains
Situation (directories tree) on an Apache server with addon domains:
main-domain.com/
|
|_ .htaccess (just an empty file, no rule in here)
|_ index.html (shown when accessing http://main-domain.com)
|
|_ addon-domain-1.com/
| |
| |_ .htaccess
| |_ index.html (shown when accessing http://addon-domain-1.com or http://main-domain.com/addon-domain-1.com/)
|
|_ addon-domain-2.com/
|
|_ .htaccess
|_ index.html (shown when accessing http://addon-domain-2.com or http://main-domain.com/addon-domain-2.com/)
Let's say in "addon-domain-1.com/.htaccess" file I have some rule using %{HTTP_HOST} like:
RewriteCond %{HTTP_HOST} ^something$
Does %{HTTP_HOST}
evaluates to the domain of the currently requested url on server???
So if asking for:
http://addon-domain-1.com/
%{HTTP_HOST} will be "addon-domain-1.com"?
http://addon-domain-1.com (without final slash)
%{HTTP_HOST} will still be "addon-domain-1.com"?
http://www.addon-domain-1.com
%{HTTP_HOST} will still be "www.addon-domain-1.com"?
And when asking for:
http://main-domain.com/addon-domain-1.com
%{HTTP_HOST} will be "main-domain.com"???
or "main-domain.com/addon-domain-1.com"???
The HTTP_HOST is obtained from the HTTP request header and this is what the client actually used as "target host" of the request. The SERVER_NAME is defined in server config.
The HTTP_HOST server variable contains www.mysite.com . The SERVER_PORT server variable contains 80 . The SERVER_PORT_SECURE server variable contains 0 and HTTPS contains OFF . The REQUEST_URI server variable contains /content/default.
mod_rewrite provides a flexible and powerful way to manipulate URLs using an unlimited number of rules. Each rule can have an unlimited number of attached rule conditions, to allow you to rewrite URL based on server variables, environment variables, HTTP headers, or time stamps.
$1 represents the match from the first set of parentheses in the RewriteRule regex, not in the RewriteCond regex.
You pretty much guessed them all right! The last one would be;
main-domain.com
%{HTTP_*}
evaluates to the HTTP header with the name given after the prefix shown. In HTTP 1.1, the host being accessed is given in the Host
header, so yes.
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