Php htaccess:同一页面的不同url不';行不通
我在htaccess文件中有以下行:Php htaccess:同一页面的不同url不';行不通,php,.htaccess,Php,.htaccess,我在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。
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
。
你知道如何解决这个问题吗?你的(.*)
匹配在第一行,它将匹配你的两个场景
试试这个,在以下情况下它对我有效:
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的格式是否正确。您希望所有URL都像http://www.example.com/news/test
或解析为index.php?page=news&article=test
解析为index.php?page=news&article=test
我的解决方案对你有用吗?你能像我提到的完整URL那样粘贴准确的示例吗?你的URL顺序匹配错误请仔细检查我的订单。您必须首先匹配RewriteRule^(.*)/(.*)$/index.php?page=$1&article=$2[NC,L]
,然后您必须匹配RewriteRule^(.*)$/index.php?page=$1[NC,L]
,您还必须添加[L]
,以确保它是最后一条规则。它仍然不起作用。一旦我删除RewriteRule^(.*)$index.php?page=$1[NC]
它适用于``和http://www.example.com/news/
但http://www.example.com/news
或“`重写规则^(.*)$index.php?页面=$1[NC]
需要是最后一个规则,因为它匹配所有条目,并确保添加[L]
和[NC,L]
,因为它将停止匹配进一步的规则,否则它将继续匹配其他规则,直到匹配最后一个条件。遇到相同的错误,我只是尝试对这两个参数使用指令,但对原因感到好奇?!为什么同时使用这两个规则会产生一些错误?@kaveh第一个规则是^(.*)$
,它匹配您的案例中域名后的任何内容新闻/测试
。现在,如果您知道了的话。你会接受我的答案并接受它吗:)