How can I add a global Apache rewrite rule?
Can I add global Apache rewrite rule, but not restrict the rule to a specific directory?
For example, add a rewrite rule applied both under 开发者_StackOverflow"/" and /news/.
Here you go:
Do not execute the rule if the trailing word is a directory
RewriteCond %{REQUEST_FILENAME} !-d
Do not execute the rule if the directory (dir1 or dir3 or dir4) found in the URL
RewriteCond %{REQUEST_FILENAME} [^(dir1|dir3|dir4)]
Again, you can test your rewrite rule here: http://martinmelin.se/rewrite-rule-tester/
Rewrite rules can be applied not only in <Directory>
sections or .htaccess
files, but also in the VirtualHost definition. They are even faster there because fewer jobs are needed.
You'll only get some problems if you want to play with the final file or directory name on the RewriteCond (as it's not yet decided), but you can sometime play with late filling of the conditions. Check for LA-U keyword in this documentation.
Now, this also means you cannot do that in a .htaccess as it's a configuration hook of a <Directory>
section. Except for one thing; on a .htaccess ina subdirectory you can set the RewriteOptions inherit
which means the rules of the parent directory are applied (so any rule set in the .htaccess or Directory section of the parent).
精彩评论