我正在尝试使用
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
答案 0 :(得分: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}
正如Daniel所说,你也可以使用mod_rewrite qsappend
或QSA
标志来达到这个目的。参考: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]
答案 1 :(得分:1)
设置QSA flag以使请求的查询自动附加到替换网址中的一个:
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。