mod_rewrite将所有请求重定向到index.php,包括目录

时间:2014-05-20 10:30:55

标签: .htaccess url mod-rewrite url-rewriting rewrite

我想将所有请求重定向到index.php。

例如:localhost/abc/def ---> localhost/index.php?url=abc/def

这是我的.htaccess行:

RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

但我有问题,我有一个目录“test”,当我转到localhost/test时,我希望它重定向到locahost/index.php?url=test,但地址栏会显示localhost/test/?url=test

如何删除查询字符串? (?url=test或类似的东西,当我输入访问目录的地址时)?

1 个答案:

答案 0 :(得分:3)

这是因为mod_dir在重写规则之后运行,并添加了重定向以添加尾部斜杠。

你可以在你的root .htaccess:

中拥有它
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*?)/?$ index.php?url=$1 [QSA,L]

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+?)/$ index.php?url=$1 [QSA,L]
相关问题