Forum Moderators: phranque

Message Too Old, No Replies

Order of code in htaccess?

         

JAB Creations

5:15 am on Nov 25, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I can't seem to find the older threads that clarified what should come before what in the .htaccess file. I remember that specific redirects come before rewrites.

What I have are...

Redirects
Rewrite
AddType
Header cache control for media files
Deny From

The redirects are all specific. Should it be in that general order or different?

- John

wilderness

5:53 pm on Nov 25, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month





How about the "Forum Library" at page top?

And specifically this reference; Proper Order for htaccess [webmasterworld.com]

g1smd

9:07 pm on Nov 25, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The .htaccess file is processed "per module":

mod_auth will process only the rules that apply to it.
mod_rewrite will process only the rules that apply to it.

It is useful to group all the rules together for one module, and separately all the rules for another module.

The overall order isn't important, only the order of the rules that one module processes.

For mod_rewrite, I would place rules which bar access first. Next comes the rules which redirect to a different URL. Last are any internal rewrites.

External redirects must always be before internal rewrites, otherwise you risk exposing server internal filepaths back onto the web as URLs.

Place rules which bar access first, before any redirects. There's no point redirecting the first request only to block the redirected second request.

JAB Creations

12:42 am on Nov 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for the replies, I do group all similar rules and figured that if I use a password it should come before anything else. The redirects would obviously come before the rewrite. I'm not sure about the DENY FROM though, before the redirects or...? I have a lot of those. ;) I am guessing AddType goes after redirects and deny from though before the rewrite? It did not cross my mind to check for the forum library though I'll try to keep that in mind next time.

- John

g1smd

6:11 am on Nov 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



DENY FROM is processed by mod_auth and is not processed by mod_rewrite, so the order relative to the mod_rewrite rules is irrelevant.

AddType is processed by mod_mime and is not processed by mod_rewrite, so the order relative to the mod_rewrite rules is irrelevant.

Apache runs each module in turn in a predefined order defined elsewhere in the apache configuration. Each module reads only the rules that apply to it, irrespective of where they are within the .htaccess file.