【问题标题】:Htaccess Mod-Rewrite to Rewrite File PathHtaccess Mod-Rewrite 重写文件路径
【发布时间】:2011-12-16 16:58:46
【问题描述】:

我想重写来自:

的调用

http://domain.com/index.php?var1=value&var2=alpha&var3=numeric

收件人:

http://domain.com


我已经尝试了以下各项,但均未成功。 您能提出更好的解决方案吗?

//#1
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^index\.php\?var1=value&var2=[^&]*&var3=[^&]*$ http://domain.com [R=301]

//#2
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^index\.php\?var1=value&var2=([A-Za-z-]+)&var3=([0-9-]+)$ http://domain.com [R=301]

【问题讨论】:

    标签: apache .htaccess mod-rewrite


    【解决方案1】:

    你不能匹配重写器中的查询字符串。

    最好用这个:

    # match 'value' (not sure if this is static, otherwise change it)
    RewriteCond %{QUERY_STRING}   (^|&)var1=value           [NC]
    # match all characters from A to Z (case insensitive), including -
    RewriteCond %{QUERY_STRING}   (^|&)var2=([A-Z-]+)       [NC]
    # match all numbers, including -
    RewriteCond %{QUERY_STRING}   (^|&)var3=([0-9-]+)$      [NC]
    # if all RewriteCond's are satisfied, this RewriteRule will be executed
    RewriteRule ^index\.php$      http://domain.com         [R=301,L]
    

    这些 RewriteCond 将被“与”运算,我只是将它们分开,以便您可以看到它们在做什么,而且即使参数以不同的顺序给出,也可以匹配查询字符串。

    【讨论】:

    • 由于某种原因,您的答案产生了 -> domain.com/?var1=value&var2=alpha&var3=numeric。如果在替换 URL 字符串的末尾添加问号,则会生成 domain.com。这就是我想要的,但我不确定这是否是正确的方法。
    • @rrrfusco 我已经编辑了我的答案。请试一试。另外,我不明白“如果您在替换 URL 字符串的末尾添加问号”是什么意思。URL 中应该只有一个问号,之后的所有内容都将被解释为查询字符串
    • Re: 添加问号,RewriteRule ^index\.php$ domain.com? [R=301,L] 我相信不贪婪的修饰符 '?'停止字符串。见webdeveloper.com/forum/showthread.php?t=192038 Post#6
    • @rrrfusco 我的 RewriteConds 中没有不贪婪的修饰符,所以这应该不是问题。此外,由于我只匹配字母数字字符,因此不会匹配超出变量的值边界。您是在将我的确切示例复制到您的 .htaccess 中,还是在修改它?
    猜你喜欢
    • 1970-01-01
    • 2015-09-22
    • 1970-01-01
    • 2016-02-02
    • 1970-01-01
    • 2013-03-24
    • 1970-01-01
    • 2013-11-15
    • 1970-01-01
    相关资源
    最近更新 更多