0

我在 htaccess 文件中有这些行:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [NC]
RewriteRule ^(.*)/(.*)$ index.php?page=$1&article=$2 [NC]

当涉及到这样的地址时/news/test,页面变量会是这样的index.php。知道如何解决吗?

4

1 回答 1

1

(.*)第一行匹配,这将匹配您的两个场景。

试试这个,它在以下情况下对我有用:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ /index.php?page=$1&article=$2 [L]
RewriteRule ^(.*)$ /index.php?page=$1 [L]

它适用于:

http://www.example.com/news/test -> http://www.example.com/index.php?page=news&article=test
http://www.example.com/news/ -> http://www.example.com/index.php?page=news&article=
http://www.example.com/news -> http://www.example.com/index.php?page=news

让我知道,如果您需要做其他事情。为了正确测试它,我添加了[L,R=302]而不是[L]查看 URL 是否正确。

于 2013-05-15T06:34:28.580 回答