.htaccess用问号“?”重写URL

时间:2013-08-31 00:20:36

标签: .htaccess url mod-rewrite

我的目标是这个网址:

component/users/?view=registration

要:

registration.html

.htaccess位于网站的mysite文件夹中。

我试过这个:

RewriteBase /mysite
RewriteRule ^component/users/?view=registration$ registration.html$ [R=301,L]

但我不工作......

当我尝试这个时:

RewriteRule ^component/users/_view=registration$ registration.html$ [R=301,L]

效果很好。

那我怎么能用问号解决这个问题呢。我已经读过it is not a part of the URL(附后)。我已经读过我必须使用querystring之类的东西,但我并不真正理解语法。

也许有人可以写出这个问题的解决方案?会很棒=)

2 个答案:

答案 0 :(得分:23)

您需要使用%{QUERY_STRING}来捕获查询字符串数据:

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

如果路径component/users匹配,则上述规则/条件将采用查询字符串视图的值并使用它来形成重定向。

答案 1 :(得分:-3)

RewriteRule ^component/users/\?view=registration$ registration.html$ [R=301,L]

你需要一个\因为?是正则表达式的一部分。要将它用作字符串,您需要使用\

来转义它

Linktip:http://ole.michelsen.dk/tools/regex.html

相关问题