2013-05-15 20 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

相關問題