【问题标题】:How to use htaccess to redirect all but one subdirectory如何使用 htaccess 重定向除一个子目录外的所有子目录
【发布时间】:2010-10-29 21:09:24
【问题描述】:

自从我弄乱 .htaccess 以来已经有一段时间了,我似乎无法完全正确。我有一个站点,例如 example.com,我希望 example.com/* 重定向到 example.com/collector.html except 以获取我想要的子目录 example.com/special 下的 URL继续工作。

例如:

  • example.com/page.html 应该重定向到 example.com/collector.html
  • example.com/foo/bar 应该重定向到 example.com/collector.html
  • example.com/special/page.html 不应重定向

我本来以为是这样的

RedirectMatch 302 ^/[^(special)]/.* /collector.html
RedirectMatch 302 ^/[^(collector.html)/]* /collector.html

可以解决问题,但它似乎没有按我想要的方式工作。

【问题讨论】:

    标签: .htaccess redirect mod-alias


    【解决方案1】:

    也许是这个? (需要 mod_rewrite)

    RewriteEngine On
    RewriteRule !^(special(/.*)?|collector\.html)$ collector.html [R,L]
    

    【讨论】:

    • 我想过这个,但我认为他想用 302 代码明确地重定向用户......
    • 这就是 [R] 标志的作用
    • 不得不做一个小修改,把“special/”改成“special/?”这样“example.com/special”就不会重定向,但除此之外,谢谢!
    • @Tyler:您可能希望将正则表达式部分设为“!^(special(/.*)?|collector\.html)$”,因为您的版本不会重定向到“ example.com/specialpage.html"。我将编辑我的答案。
    【解决方案2】:

    这对我来说在 Apache 2.2.13 上运行良好,我在其中重新创建了您描述的目录结构。

    RedirectMatch 302 /(?!special)  http://www.example.com/collector.html
    

    括号中的模式表示如果 URI 包含字符串 'special',则不匹配。

    mod 重写并不总是可用的,在这种情况下不需要。

    【讨论】:

      【解决方案3】:

      试试这个:

      RedirectMatch 302 /\/[^(special)]\/.+/ /collector.html 
      RedirectMatch 302 /\/[^(collector\.html)]/ /collector.html
      

      【讨论】:

      • 这可以让所有子目录进入收集器,但不能重定向根目录中的文件,例如example.com/page.html
      • 发生了什么?页面是否在没有任何重定向的情况下正确检索?
      • 在同一行试试这个:RedirectMatch 302 /\/[^(special\/)|(collector\.html)].*/ /collector.html
      【解决方案4】:

      也许……?

      RewriteEngine on
      RewriteRule ^(?!special) collector.html [R,NC]
      

      【讨论】:

        猜你喜欢
        • 2014-07-23
        • 1970-01-01
        • 1970-01-01
        • 2013-09-23
        • 2016-11-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-24
        相关资源
        最近更新 更多