Mod-rewrite shenanigans

Bin*_*les 2 mod-rewrite

我有这个重写规则

RewriteEngine On
RewriteBase /location/
RewriteRule ^(.+)/?$ index.php?franchise=$1
Run Code Online (Sandbox Code Playgroud)

这是假设要更改此URL

http://example.com/location/kings-lynn
Run Code Online (Sandbox Code Playgroud)

进入这一个

http://example.com/location/index.php?franchise=kings-lynn
Run Code Online (Sandbox Code Playgroud)

但相反,我得到了这个

http://example.com/location/index.php?franchise=index.php
Run Code Online (Sandbox Code Playgroud)

此外,添加栏杆斜线会打破它.我得到index.php页面显示但没有样式表或javascript正在加载.

我显然做了一些非常错误但我不知道尽管在这里花了一整天R'ingTFM和许多在线引物和教程和问题.

Ken*_*ric 5

你的问题是你重定向两次.

'location/index.php' 匹配正则表达式 ^(.+)/?$

您希望可能使用"if file not exists"条件使其不再尝试映射.

RewriteEngine on
RewriteBase /location/
RewriteCond %{REQUEST_FILENAME} !-f   # ignore existing files
RewriteCond %{REQUEST_FILENAME} !-d   # ignore existing directories
RewriteRule ^(.+)/?$ index.php?franchise=$1  [L,QSA]
Run Code Online (Sandbox Code Playgroud)

另外,还有[L,QSA]试图使其成为"最后"规则(注意,这并不完全明显它是如何工作的)并将查询字符串附加到查询中,以便

location/foobar/?baz=quux  ==> index.php?franchise=$1&baz=quux
Run Code Online (Sandbox Code Playgroud)

( 我认为 )