Hidden features of mod_rewrite

2019-01-02 16:34发布

There seem to be a decent number of mod_rewrite threads floating around lately with a bit of confusion over how certain aspects of it work. As a result I've compiled a few notes on common functionality, and perhaps a few annoying nuances.

What other features / common issues have you run across using mod_rewrite?

8条回答
笑指拈花
2楼-- · 2019-01-02 17:10

if you need to 'block' internal redirects / rewrites from happening in the .htaccess, take a look at the

RewriteCond %{ENV:REDIRECT_STATUS} ^$

condition, as discussed here.

查看更多
无与为乐者.
3楼-- · 2019-01-02 17:10

Other Pitfalls:

1- Sometimes it's a good idea to disable MultiViews

Options -MultiViews

I'm not well verse on all of MultiViews capabilities, but I know that it messes up my mod_rewrite rules when active, because one of its properties is to try and 'guess' an extension to a file that it thinks I'm looking for.

I'll explain: Suppose you have 2 php files in your web dir, file1.php and file2.php and you add these conditions and rule to your .htaccess :

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ file1.php/$1 

You assume that all urls that do not match a file or a directory will be grabbed by file1.php. Surprise! This rule is not being honored for the url http://myhost/file2/somepath. Instead you're taken inside file2.php.

What's going on is that MultiViews automagically guessed that the url that you actually wanted was http://myhost/file2.php/somepath and gladly took you there.

Now, you have no clue what just happened and you're at that point questioning everything that you thought you knew about mod_rewrite. You then start playing around with rules to try to make sense of the logic behind this new situation, but the more you're testing the less sense it makes.

Ok, In short if you want mod_rewrite to work in a way that approximates logic, turning off MultiViews is a step in the right direction.

2- enable FollowSymlinks

Options +FollowSymLinks 

That one, I don't really know the details of, but I've seen it mentioned many times, so just do it.

查看更多
登录 后发表回答