2013-05-15 23 views
0

我得在htaccess文件这些行:htaccess的:对于同一个页面不同的URL不起作用

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。 任何想法如何解决?

+0

你希望所有的URL像'http:// www.example.com/news/test'或解析为'index.php?page = news&article = test' –

+0

解析为'index.php?page = news&article =测试' – Kaveh

+0

我的解决方案是否适合您? –

回答

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是否正确生成。

+0

Tnx的答案。它仍然不起作用。 (。*)?当我删除 '重写规则^ $ index.php页面= $ 1 [NC]' 它适用于 ''http://www.example.com/news/test和 'HTTP: //www.example.com/news/' ,但错误404为 'http:// www.example.com/news'或 'http:// www.example.com/news/test /' – Kaveh

+0

'RewriteRule ^(。*)$ index.php?page = $ 1 [NC]'必须是最后一条规则,因为它匹配所有条目,并且确保用'[NC,L]'原因添加'[L]'它将停止匹配更多的规则,否则它将继续匹配其他规则,直到它匹配的最后一个条件。 –

+0

遇到同样的错误,我只是尝试使用两个参数的指令,但好奇的原因?!为什么使用这两个规则会犯一些错误 – Kaveh

相关问题