【问题标题】:.htaccess rewrite URL with a question mark "?".htaccess 用问号“?”重写 URL
【发布时间】:2013-09-03 18:58:17
【问题描述】:

我的目标是这个网址:

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(附加)。我读过我必须使用类似查询字符串的东西,但我并没有真正理解语法。

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

【问题讨论】:

    标签: .htaccess url mod-rewrite


    【解决方案1】:
    RewriteRule ^component/users/\?view=registration$ registration.html$ [R=301,L]
    

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

    对其进行转义

    链接提示:http://ole.michelsen.dk/tools/regex.html

    【讨论】:

    • 不起作用。它仍然将我重定向到 localhost/mysite/component/users/?view=registration :(
    【解决方案2】:

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

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

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

    【讨论】:

    猜你喜欢
    • 2021-08-21
    • 1970-01-01
    • 2021-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-02
    • 2013-12-24
    • 1970-01-01
    相关资源
    最近更新 更多