|Preferred order for various rewrites|
| 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?
| 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
[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.
| 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.
| 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.
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.
| 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.