Forum Moderators: phranque
For instance, can you put "#" as a line separator, between rules and conditions? or to add comments?
Mine starts off with <Files 403.shtml>,
then goes to a bunch of IP deny lines,
followed by Redirect permanents,
the it has Options +FollowSymlinks
RewriteEngine on
RewriteCond
RewriteRule
RewriteCond
RewriteRule, etc.,
then I had a no hotlinking entry, but removed it to clean it up (it was blocking Googlebot and MSN!)
It's hard to read, and I know a couple of rewrite conditions have to be in a specific order, but should the RewriteEngine on be higher on the list? Redirects before denys? How many spaces between the rewrite conditon and the [NC], etc.?
I've looked all over the Internet, and there isn't a good guide that explains how this should be done.
It makes no difference what order you put all of your mod_rewrite, mod_alias, and other directives. That is, you can put all of your Redirect (mod_alias) directives first, followed by all of your mod_rewrite directives, followed by all of the directives for other modules.
Changing the order of these groups will have no effect. Only changing the order of the directives inside the groups will have an effect.
This is because your .htaccess file is not a program, or even a simple script to be interpreted. It is actually a collection of scripts, each for different Apache modules.
The Apache modules will execute in whatever order the server has been configured to execute them. The method depends on the version of Apache. Each module will run in the order set by the server config, and read your .htaccess file, executing only the directives that it recognizes and understands.
So, your .htaccess file is not executed in a strictly beginning-to-end order, but rather like a group of subroutines called by the server from outside the file.
So, it makes no difference whether you write
Redirect 301 /index.html http://www.example.com/index.php
RewriteRule (.*)\.php http://www.example.com/index.asp [R=301,L]
RewriteRule (.*)\.php http://www.example.com/index.asp [R=301,L]
Redirect 301 /index.html http://www.example.com/index.php
To answer your specific question, RewriteEngine on should always come before any other mod_rewrite directives.
Because the Options directive belongs to Apache core, rather than to mod_rewrite, it will always be executed before any mod_rewrite directives, regardless of where you put it in your .htaccess file.
I hope this is clear, because it's hard to describe.
I guess the best example would be an instruction book that includes instructions in 20 languages. Readers will not really care what order the language-sections are in. All that is important is the the paragraphs and words in each language section are in the right order. So, to complete the analogy, the readers are Apache modules, and the paragraphs within each language section are the directives for each Apache module. Only the server configuration in httpd.conf or conf.d will determine the order that the different language-speakers pick up and read the instructions.
Jim
Comments should be on their own lines, not be added to the end of directives; Some modules will throw an Apache 'Warning' if comments are appended to directives, and this can clutter up your error log if you set the error level to its most verbose mode to debug a tricky problem.
"#" characters on blank lines are optional.
Jim