Mod-rewrite Trailing Slash Issue

China☆狼群 提交于 2019-12-12 10:49:11

问题


There doesn't seem to be much info on this topic so I'm going to outline my specific problem then maybe we can shape the question and the answer into something a bit more universal.

I have this rewrite rule

RewriteEngine On
RewriteBase /bookkeepers/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/?$ index.php?franchise=$1

Which is changes this URL

http://example.com/location/kings-lynn

Into this one

http://example.com/location/index.php?franchise=kings-lynn

The problem I am having is that if I add a trailing slash

http://example.com/location/kings-lynn/

then the query string is returned as

franchise=kings-lynn/

and for some reason none of my CSS and Javascript files are being loaded.

Any ideas?


回答1:


As @Paul Tomblin said, the .+ is being greedy; that is, it's matching as much as it can.

^(.+[^/])/?$ tells it to match anything, followed by a character that isn't a /, then followed by an optional /. This has the effect of not capturing the trailing /.

The most probable reason your CSS and Javascript doesn't work is you're using a relative path, like src="my.js". When there's a trailing slash, it looks like a directory, so your browser will look for /location/kings-lynn/my.js. You can fix this simply by using an absolute path to your files (e.g. /location/my.js).




回答2:


It looks like the (.+) is being greedy matched. In that case, you could try

RewriteRule ^(.+[^/])/?$ index.php?franchise=$1

This makes sure that the first group (in the brackets) doesn't end in a slash.



来源:https://stackoverflow.com/questions/314872/mod-rewrite-trailing-slash-issue

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