.htaccess url rewrite behavior being overwritten if filename minus ext. same as url

▼魔方 西西 提交于 2019-12-12 14:46:39

问题


I'm trying to tidy up the URLs and remove the .php extensions from them and such. I'm in the base folder for the website, so there are no parent .htaccess files that could be taking priority or anything. Here's my htaccess code.

RewriteEngine On
RewriteRule    ^give/?$    give.php    [NC,L]

This part gives no real problems, because whatever behavior is overwriting it behaves the same way. But when I add in this other line so it will account for url variables,

RewriteRule    ^give/([0-9]+)/?$    give.php?step=$1    [NC,L]

It completely ignores it. However, if I rename give.php to anything else, so it doesn't match the url, it works. For example, using given.php causes it to heed the .htaccess rules. I've never run across this issue before. Is there some server setting I can change so I don't have to give my files odd names?

Also note, I tried changing that first line to redirect to another file, but without changing the actual file name give.php. It ignored my redirect and still loaded the give.php file.

Edit: I've tried changing the order of the 2 rules, I've tried commenting both one rule, then the other to see if they are conflicting with eachother. The best I can figure, the server has some sort of default behavior that directs /give/ to /give.php if no directory of /give/ exists. Because even if I remove both rules, going to /give/ still redirects me to /give.php. Only when I change the filename to given.php will it break that default behavior. I also tried setting the simpler rule to this:

RewriteRule    ^give/?$    resources.php    [NC,L]

And as long as the file give.php existed, it still redirected to give.php. If I removed give.php or changed its name, going to /give would then redirect to resources.php


回答1:


You may have a conflict with the MultiViews option of mod_negotiation. Your request may not be hitting the RewriteRule at all, and is instead being handled by the give.php because of mod_negotiation. From the docs:

... if the server receives a request for /some/dir/foo, if /some/dir has MultiViews enabled, and /some/dir/foo does not exist, then the server reads the directory looking for files named foo.*, and effectively fakes up a type map which names all those files, assigning them the same media types and content-encodings it would have if the client had asked for one of them by name. It then chooses the best match to the client's requirements.

Try adding this to your .htaccess and see if things change:

Options -MultiViews



回答2:


So some changes, first I would try some file conditions before the rule. Second the params as you wrote are only for numbers, and last QSA for more query.

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^give/(.+)$ give.php?step=$1 [QSA, L]


来源:https://stackoverflow.com/questions/47895539/htaccess-url-rewrite-behavior-being-overwritten-if-filename-minus-ext-same-as

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!