【问题标题】:htaccess rewrite match question markhtaccess 重写匹配问号
【发布时间】:2013-10-09 03:33:58
【问题描述】:

我需要关于重写规则和正则表达式的帮助

这是我的输入网址

http://localhost/ws/rest/user/login?login=user_login&password=user_pass

我希望它重定向到这个

http://localhost/index.php?type=rest&ressource=user&action=login&login=user_login&password=user_pass

我的重写规则

RewriteCond %{REQUEST_METHOD} ^(GET|POST)
RewriteRule ^ws/(.+)/(.+)/([A-Za-z]+)(\\?)(.+)?   index.php?type=$1&ressource=$2&action=$3&$5 [L]

我认为问题出在 url 中的 ? 周围,因为当我用 & 替换时它可以工作?

【问题讨论】:

    标签: regex .htaccess mod-rewrite url-rewriting


    【解决方案1】:
    RewriteEngine On
    
    RewriteCond %{REQUEST_METHOD} ^(GET|POST)
    RewriteRule ^ws/([^/]+)/([^/]+)/([A-Za-z]+) index.php?type=$1&resource=$2&action=$3 [QSA,L]
    

    注意QSA 标志,以便将来自原始 URL 的查询字符串附加到新 URL。

    【讨论】:

      【解决方案2】:

      问题: QUERY_STRINGRewriteRule 不匹配。用这个替换你的代码:

      RewriteCond %{QUERY_STRING} ^login=user_login&password=user_pass$ [NC]
      RewriteRule ^ws/([^/]+)/([^/]+)/([a-z]+)/?$ /index.php?type=$1&ressource=$2&action=$3 [L,QSA,NC]
      

      由于此处使用了QSA 标志,您现有的QUERY_STRING 将附加到生成的URI。

      在此处了解更多信息:Apache mod_rewrite Introduction

      【讨论】:

        猜你喜欢
        • 2015-09-24
        • 1970-01-01
        • 1970-01-01
        • 2013-09-03
        • 2021-08-21
        • 1970-01-01
        • 1970-01-01
        • 2023-04-03
        • 2015-03-21
        相关资源
        最近更新 更多