homepage Welcome to WebmasterWorld Guest from 54.196.63.93
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
How Does Apache Decide Order to Execute Modules?
Dideved



 
Msg#: 4572958 posted 3:19 am on May 11, 2013 (gmt 0)

I have a question for y'all. Modules such as mod_dir, mod_alias, mod_rewrite... how does Apache determine what order to process them?

The most intuitive guess -- that it's determined by the order the modules are loaded -- doesn't seem to be the case. I've tried making mod_rewrite the first module load, and I've tried making it the last module loaded, but neither seems to make any difference.

I've also sifted through the documentation looking for the answer, but I haven't found it yet.

 

DrDoc

WebmasterWorld Senior Member drdoc us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4572958 posted 5:00 am on May 11, 2013 (gmt 0)

In many cases, it's actually the order in which they are loaded. In some cases, a module depends on another being loaded first, in which case execution may be stalled.

Dideved



 
Msg#: 4572958 posted 5:36 am on May 11, 2013 (gmt 0)

Any chance you could provide some sort of verifiable test or documentation? I ask because mod_rewrite redirections will execute before mod_alias redirections, regardless of their order in the configuration file, yet neither of these modules depend on the other.

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4572958 posted 7:32 am on May 11, 2013 (gmt 0)

In many cases, it's actually the order in which they are loaded.

I had the impression this changed between 1.3 and 2.2-- and I think I've hunted down the source [webmasterworld.com] of this impression.

Don't look at me. I didn't pick that shade of yellow.

I'm sure he said the same thing many other times over the years, but this is the one that bobbed to the surface. It helps explain why apache docs themselves are so stubbornly elusive in regard to what would seem like a common and obvious question: It's because the correct and complete answer is "It depends" ;)

Dideved



 
Msg#: 4572958 posted 8:42 am on May 11, 2013 (gmt 0)

Thanks. That helped me figure out what I should be looking for.

Here's where the documentation explains calling order:
httpd.apache.org/docs/2.4/developer/hooks.html#hooking-order

For the uber curious, here's where mod_rewrite defines its calling order (line 5109):
svn.apache.org/viewvc/httpd/httpd/trunk/modules/mappers/mod_rewrite.c?view=markup#l5109
That mod_rewrite defines itself as hook_first and mod_alias defines itself as hook_middle is why rewrite directives happen before redirect directives.

mod_info provides a much more readable way to see the priority of each module for each hook:
Translate Name:
00 mod_rewrite.c
10 mod_alias.c
30 core.c

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 4572958 posted 12:43 pm on May 11, 2013 (gmt 0)

there's also this:

http://httpd.apache.org/docs/current/rewrite/avoid.html#redirect
The use of RewriteRule to perform this task may be appropriate if there are other RewriteRule directives in the same scope. This is because, when there are Redirect and RewriteRule directives in the same scope, the RewriteRule directives will run first, regardless of the order of appearance in the configuration file.

Dideved



 
Msg#: 4572958 posted 5:20 pm on May 11, 2013 (gmt 0)

> there's also this:

Absolutely. In fact, that's the paragraph that got me asking this question. I wanted to know _why_ one always runs before the other, and how Apache decides that order.

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4572958 posted 7:40 pm on May 11, 2013 (gmt 0)

Note that there are two more values, APR_HOOK_REALLY_FIRST and APR_HOOK_REALLY_LAST.

:: snrk ::

I guess this is the key part:
When a module knows that it must be run before (or after) some other modules, it can specify them by name. The second (third) argument is a NULL-terminated array of strings consisting of the names of modules that must be run before (after) the current module. For example, suppose we want "mod_xyz.c" and "mod_abc.c" to run before we do, then we'd hook as follows:
static void register_hooks()
{
static const char * const aszPre[] = { "mod_xyz.c", "mod_abc.c", NULL };
ap_hook_do_something(my_something_doer, aszPre, NULL, APR_HOOK_MIDDLE);
}


Which all comes back to this forum's boilerplate: If you are on shared hosting, you probably don't know and definitely can't change the modules' execution order.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved