2010-09-27 21 views
2

我想使用 http://www.example.com/news/id/21/title/top-10-things/?page= 1發送的頁面參數,它不是在PHP

下面

工作是我的.htaccess文件中設置

Options +FollowSymLinks 
RewriteEngine on 
RewriteRule ^news/(.*)/(.*)/(.*)/(.*)/$ /news.php?$1=$2&$3=$4 
RewriteRule ^news/(.*)/(.*)/$ /news.php?$1=$2 
RewriteRule ^news/$ /news.php 
+1

什麼不工作? 'page'不包含在'$ _GET'中或者什麼? – 2010-09-27 14:12:40

回答

3

嘗試追加%{QUERY_STRING}的網址,在你的htaccess。

Options +FollowSymLinks 
RewriteEngine on 
RewriteRule ^news/(.*)/(.*)/(.*)/(.*)/$ /news.php?$1=$2&$3=$4&%{QUERY_STRING} 
RewriteRule ^news/(.*)/(.*)/$ /news.php?$1=$2&%{QUERY_STRING} 
RewriteRule ^news/$ /news.php&%{QUERY_STRING} 

正如丹尼爾提到的,你也可以使用mod_rewrite的qsappendQSA標誌用於這一目的。 REF:http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html

Options +FollowSymLinks 
RewriteEngine on 
RewriteRule ^news/(.*)/(.*)/(.*)/(.*)/$ /news.php?$1=$2&$3=$4 [QSA] 
RewriteRule ^news/(.*)/(.*)/$ /news.php?$1=$2 [QSA] 
RewriteRule ^news/$ /news.php [QSA] 
+3

您可以改爲使用[QSA](查詢字符串附加)標誌。 – 2010-09-27 14:15:35

1

設置QSA flag獲得自動追加到一個在替換URL請求的查詢:

RewriteRule ^news/(.*)/(.*)/(.*)/(.*)/$ /news.php?$1=$2&$3=$4 [QSA] 
RewriteRule ^news/(.*)/(.*)/$ /news.php?$1=$2 [QSA] 
RewriteRule ^news/$ /news.php [QSA] 

而且,你不應該使用.*如果你能更具體。在這種情況下使用[^/]+代替避免了不必要的回溯:

RewriteRule ^news/([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ /news.php?$1=$2&$3=$4 [QSA] 
RewriteRule ^news/([^/]+)/([^/]+)/$ /news.php?$1=$2 [QSA] 
RewriteRule ^news/$ /news.php [QSA] 

以及用於任意數目或參數的一般的解決方案,請參見Rewriting an arbitrary number of path segments to query parameters

相關問題