使用?重写URL?符号

时间:2011-01-04 03:37:57

标签: regex apache .htaccess

我尝试重新编写具有以下格式的网址:

website.com/?q=home

到这种形式:

website.com/home

我找不到。

我试过这个表格,例如: RewriteRule ^ \?q \ = home $ / home [R = 301,L]

看起来像“?”或者“=”正在构成问题?你能救我吗?

谢谢

1 个答案:

答案 0 :(得分:1)

?之后的所有内容都在变量%{QUERY_STRING}中。

仅重写值home使用:

RewriteCond %{QUERY_STRING} ^q=home$
RewriteRule ^ /home? [L,R=301]

要将所有q值重写为静态URI,请使用:

RewriteCond %{QUERY_STRING} ^q=(.*)$
RewriteRule ^ /%1? [L,R=301]

最后?在RewriteRule中,从目标URI中删除查询字符串。

参考:Apache mod_rewrite

相关问题