问题
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]
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
Why is it not item1/item2/item3...........
?
Strangely, if the URL is www.domain.com/item1/item2/item3..........a
, the route is what I expect, item1/item2/item3..........a
I've tried a few other regular expressions, but they act similarly.
Thank you.
回答1:
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.
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'], "/" );
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.
回答2:
I've got the same problem and it seems like a bug of Apache on Windows platform. However, it works on Linux.
https://issues.apache.org/bugzilla/show_bug.cgi?id=48687
来源:https://stackoverflow.com/questions/11144079/apache-mod-rewrite-ending-periods