我想更好地了解Apache的mod_rewrite,和我有意想不到的效果。我尝试过很多不同的EX pressions,但似乎没有任何表现我怎么知道它应该。
I am trying to better understand Apache's mod_rewrite, and am having unexpected results. I've tried many different expressions, but nothing seems to behave how I understand it should.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?route=$1 [QSA,L]
使用 www.domain.com/item1/item2/item3
收益率 ITEM1 / ITEM2 /项目3
使用 www.domain.com/item1/item2/item3.php
收益率 ITEM1 / ITEM2 / item3.php
使用 www.domain.com/item1/item2/item3 ...........
收益率 ITEM1 / ITEM2 /项目3
Using www.domain.com/item1/item2/item3
yields item1/item2/item3
Using www.domain.com/item1/item2/item3.php
yields item1/item2/item3.php
Using www.domain.com/item1/item2/item3...........
yields item1/item2/item3
为什么不 ITEM1 / ITEM2 /项目3 ...........
?
Why is it not item1/item2/item3...........
?
奇怪的是,如果该URL为 www.domain.com/item1/item2/item3 ..........一个
,该航线是我预计, ITEM1 / ITEM2 /项目3 ..........一个
Strangely, if the URL is www.domain.com/item1/item2/item3..........a
, the route is what I expect, item1/item2/item3..........a
我已经尝试了一些其他的常恩pressions,但他们采取同样行动。
I've tried a few other regular expressions, but they act similarly.
感谢你。
虽然我还没有找到任何文件,解释为什么阿帕奇/的.htaccess / mod_rewrite的行为就这样,我有一个选择,如果有人遇到这种
While I still haven't found any documentation explaining why Apache/.htaccess/mod_rewrite behaves this way, I do have an alternative, in case anyone comes across this.
而不是使用的.htaccess通过所要求的文件名,查询字符串变量,如在我上面的问题的路线
的,在PHP中有可以使用一些服务器变量。所以,我改变了我的逻辑的index.php读取查询字符串为:
Instead of using .htaccess to pass the requested filename into a querystring variable, like route
in my question above, in PHP there are some server variables that can be used. So, I changed my logic in index.php from reading the querystring to:
trim( $_SERVER['REDIRECT_URL'], "/" );
在REDIRECT_URL(和REQUEST_URI)服务器变量将保留圆点和装饰将得到的开头和结尾的斜杠关,类似与重写规则会发生什么。
The REDIRECT_URL (and REQUEST_URI) server variables will retain the trailing dots, and the trim will get the leading and trailing slashes off, similar to what happens with the rewrite rule.
上一篇:从与htaccess的基础URL删除变量变量、基础、htaccess、URL
下一篇:htaccess的 - 删除"的index.php"从URL和重定向重定向、QUOT、htaccess、index