且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 mod_rewrite 重写查询字符串

更新时间:2023-01-08 09:43:07

如果你真的想将 /controller/action?param1Name=param1Value 形式的请求重定向到 /controller/action/param1Name/param1Value,试试这个:

If you really want to redirect requests of the form /controller/action?param1Name=param1Value to /controller/action/param1Name/param1Value, try this:

RewriteCond %{THE_REQUEST} ^GET\ /[^/]+/[^/]+\?[^\s]+
RewriteCond %{QUERY_STRING} ^([^=&]+)=([^&]+)&?(.*)
RewriteRule ^[^/]+/[^/]+.* /$0/%1/%2?%3 [N]
RewriteCond %{THE_REQUEST} ^GET\ /[^/]+/[^/]+\?[^\s]+
RewriteRule ^[^/]+/[^/]+.* /$0 [L,R=301]

但如果你想反方向:

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