So, let’s start the story…
Request processing contexts
In the simplest case there are only two request processing contexts:
1. server context (httpd.conf), and
2. “root folder” context (.htaccess)
Server context is executed first and after that, if further processing is allowed (no redirect or proxy happened), root folder config is processed (if present).
server-wide directive
Picture 1. Server-wide configuration (httpd.conf)
per-site directive
Picture 2. Per-site configuration (.htaccess)
Picture 3. Processing order for the configs on Pictures 1 and 2
***
Now let’s make it more complicated—we’ll have the rules in the root folder and in Directory1. The processing order then becomes:
1. /.htaccess
DirectiveA
2. /Directory1/.htaccess
DirectiveB
Picture 4. Processing order in case of several .htaccess files
For the request to http://localhost/index.html only the first context is applied, while for http://localhost/Directory1/index.html (and other requests to deeper subfolders) the merged context 1+2 is executed. In our case it’s:
DirectiveA
DirectiveB
Thus, the child context complements and refines the parent one (but not the server one). This is true for nearly all Apache/Ape modules EXCEPT mod_rewrite. It’s one of a kind and behaves differently.
mod_rewrite behavior
Historically, or for convenience purposes, mod_rewrite contexts do not complement but COMPLETELY OVERRIDE each other. So, if we have two configs
1. /.htaccess
RewriteRule a b
2. /Directory1/.htaccess
RewriteRule b с
the resulting config to be applied to the request will be
RewriteRule b c
and NOT
RewriteRule a b
RewriteRule b с
which may be unobvious for newbies.
For experts! mod_rewrite has an option allowing to change this behavior and inherit the parent rules:
1. /.htaccess
RewriteRule a b
2. /Directory1/.htaccess
# inherit parent rules
RewriteOptions inherit
RewriteRule b с
makes up the following merged config:
RewriteRule b с
# parent rules are appended to the end of the merged config!
RewriteRule a b
If there are both
Picture 5. Processing order when there are both .htaccess and
Let’s see how the configs are merged for the request to http://localhost/Directory1/Directory2/index.html if each directory has both
Picture 6. Processing order when there are several .htaccess files and several
httpd.conf
DirectiveDirectoryA
DirectiveDirectoryB
DirectiveDirectoryС
/.htaccess
DirectiveA
/Directory1/.htaccess
DirectiveB
/Directory1/Directory2/.htaccess
DirectiveC
The following logics is applied to form the merged config:
1. First to execute is httpd.conf except for tagged sections:
2. Distributed config is built up from the following parts:
a. empty context is created;
b. requested URL is divided into parts like:
/
/Directory1/
/Directory1/Directory2/
c. physical path is defined for each virtual directory:
/ —> C:/inetpub/wwwroot/
/Directory1/ —> C:/inetpub/wwwroot/ Directory1/
/Directory1/Directory2/—> C:/inetpub/wwwroot/ Directory1/ Directory2/
d. corresponding section is found for each path;
e. corresponding .htaccess is found for each path
The resulting sequence of directives will be:
DirectiveDirectoryA
DirectiveA
DirectiveDirectoryB
DirectiveB
DirectiveDirectoryC
DirectiveC
Usually directives’ order is not so important, but not in case with mod_rewrite; that’s why understanding the principles of configs merging may dramatically reduce development and debugging times.
Note!
and
then both of them get into the merged config.
One should remember that everything written inside server context is applied to all requests and for all sites. Sometimes it may be necessary to limit the scope of directive to one or several sites and that’s the case to use
#httpd.conf
ServerDirective
VirtualHostDirectiveA
Note! mod_rewrite offers another way to restrict scope for the rules to specific host – RewriteCond %{HTTP_HOST}.
The difference is that RewriteCond %{HTTP_HOST} must appear before each RewriteRule, while
RewriteCond %{HTTP_HOST} localhost
RewriteRule . index.php [L]
RewriteCond %{HTTP_HOST} localhost
RewriteRule about$ about.php [L]
and
RewriteRule . index.php [L]
RewriteRule about$ about.php [L]
On the other hand, the limitation of
Picture 7. Processing order when
Note!
For request to localhost:80/page.html the second line will be executed, whereas for localhost/page.html the first one will fire.
If
Thus, the use of
Picture 8. Processing order when there are
These two behave similar to
E.g., for http://localhost/Directory1/index.html#top they will find the correspondence in file system C:\inetpub\wwwroot\Directory1\index.html and will merge all
Note!
Are applied to the corresponding virtual path, which for http://localhost/Directory1/index.html#top is /Directory1/index.html.
Summary
Let’s now put it all together. Here’s the final sequence of sections:
1. httpd.conf
2. httpd.conf
3. .htaccess
4. .htaccess
5. httpd.conf
6. httpd.conf
7. httpd.conf
8. httpd.conf
9. httpd.conf
10. httpd.conf
11. httpd.conf
12. httpd.conf
13. httpd.conf
14. httpd.conf
***
Seems every aspect of configs processing has been covered. We understand that this article may look somewhat sophisticated, but we are sure there are enthusiasts who’ll find it helpful.
No comments:
Post a Comment