【问题标题】:RewriteRule isn't working.重写规则不起作用。
【发布时间】:2013-05-30 12:08:16
【问题描述】:

重写规则不起作用

RewriteRule .*find-a-bar/london/social-eating-house. /find-a-bar/london/the-blind-pig-at-social-eating-house [R=301,L] 

当我输入 doamin.com/find-a-bar/london/social-eating-house 时没有任何反应

【问题讨论】:

    标签: .htaccess url-rewriting


    【解决方案1】:

    请注意,您的正则表达式(删除句点后)将匹配一些您可能不期望的 URI,例如

    something/find-a-bar/london/social-eating-house
    

    试试:

    ^find-a-bar/london/social-eating-house$
    

    明确说明您要匹配的字符串的开头和结尾。

    【讨论】:

      【解决方案2】:

      重写规则 .*find-a-bar/london/social-eating-house。 /find-a-bar/london/the-blind-pig-at-social-eating-house [R=301,L] 就是答案

      【讨论】:

        【解决方案3】:

        不工作的正则表达式:

        .*find-a-bar/london/social-eating-house.
        

        如您所见,您在这里使用了.* 贪婪匹配,因此 mod_rewrite 的底层正则表达式引擎可能正在使用.* 匹配完整的正则表达式。最好使用这样的非贪婪匹配模式(带有行开始和结束锚点):

        ^.*?find-a-bar/london/social-eating-house/?$
        

        你的规则应该是:

        RewriteRule ^.*?find-a-bar/london/social-eating-house/?$ /find-a-bar/london/the-blind-pig-at-social-eating-house [R=302,L,NC] 
        

        只有在您确认它工作正常后,才将R=302 替换为R=301。在测试你的 mod_rewrite 规则时避免使用R=301(永久重定向)。

        【讨论】:

          猜你喜欢
          • 2013-07-06
          相关资源
          最近更新 更多