homepage Welcome to WebmasterWorld Guest from 54.198.140.182
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

    
Preferred order for various rewrites
slipkid




msg:4498060
 3:47 am on Sep 21, 2012 (gmt 0)

My .htaccess file is composed of Rewrite Rules, both for banning and for redirection. Also have some RewriteConds.

What might be the preferred order for execution, e.g., should all the Rewrites for redirection come first, or the Rewrites that bann various lists?

What about RewriteCond? Where does it fall in the hierarchy of a properly ordered .htaccess file?

 

lucy24




msg:4498091
 6:18 am on Sep 21, 2012 (gmt 0)

All things being equal, there are two layers:

First, go from most dramatic to least dramatic. No point in redirecting someone if you're going to slam the door in their face next time they show up, two nanoseconds later. So you go

[F] 403
[G] 410
[R=301]
[L] (i.e. rewrites alone, no redirect)

Second, within each of those groups, go from most specific to most general. For example, individual page redirects go before index.html redirects which in turn go before the final with/without www. redirect.

There may be exceptions. For example, if an entire directory is 410 Gone, except for one page that you're redirecting, then obviously that redirect has to go before all the [G] rules.

What about RewriteCond? Where does it fall in the hierarchy of a properly ordered .htaccess file?

You cannot begin to imagine how worried I am by this question. It implies that you haven't got a solid grip on the relationship between RewriteConds and RewriteRules. This can easily make mincemeat of your whole redirect structure.

The Horse's Mouth [httpd.apache.org]

There are also several thousand threads in this forum talking about how RewriteRules and RewriteConds should be arranged. But you may need some assurance that we're not just making it up.

g1smd




msg:4498116
 7:24 am on Sep 21, 2012 (gmt 0)

I have nothing much to add to that answer as it covers exactly the right stuff.

I'll just mention that you will make your life much easier if you:

- add a blank line after every RewriteRule making your code easier on the eye.
- add a comment before each ruleset explaining what it does, so you have a clue what it does when you look at the code again sometime in the future.

slipkid




msg:4498444
 9:05 pm on Sep 21, 2012 (gmt 0)

Thanks Lucy24 and g1smd for your help.

Having 12 years of post grammer school education being taught via the socratic method, I learn by doing. Once exposed to a concept, I experiment.

I used to be a crackerjack FORTRAN programmer, but now my head is filled with rules for javascript, PERL, PHP, HTML4+, etc., etc. Now Apache.

Yikes!

My .htaccess file is very close to the order specified by Lucy24, and my code writing already follows the suggestions of g1smd -- lots of whitespace and loads of comments. Nevertheless, I recognize that I still have much to learn.

Read last night, Lucy24, your explanation of "query string" in another thread and it was most illuminating. I appreciate the time you and others give to educating us newcomers.

Someday, I will have a better understanding of what I am doing with Rewrite.

lucy24




msg:4498459
 9:53 pm on Sep 21, 2012 (gmt 0)

I learn by doing. Once exposed to a concept, I experiment.

Oh, excellent. That means we don't have to fire up the boilerplate on Why We Make You Do It Yourself :)

If you are already comfortable with Regular Expressions, that takes care of about 95% of mod_rewrite. The rest is just syntax.

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