.htaccess - 如何使用 htaccess 重定向除一个子目录之外的所有子目录

标签 .htaccess redirect mod-alias

自从我弄乱 .htaccess 已经有一段时间了,我似乎无法完全正确地做到这一点。我有一个站点,例如 example.com,我希望 example.com/* 重定向到 example.com/collector.html,但我想继续工作的子目录 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
    

    会做的伎俩,但它似乎并没有像我想要的那样工作。

    最佳答案

    也许这个? (需要 mod_rewrite)

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

    关于.htaccess - 如何使用 htaccess 重定向除一个子目录之外的所有子目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/925913/

    相关文章:

    .htaccess - 将非 HTTPS 重定向到 HTTPS

    php - 从登录页面重定向登录用户

    apache - 带加号的 URL 重定向

    apache - Apache禁止使用Alias 403

    .htaccess - 拒绝时重定向 |访问

    linux - 重定向域但保留原始 url

    .htaccess - htaccess 将子目录的 http 重定向到 https

    .htaccess : http to https redirection does not work

    基于子域的Apache重写

    regex - 在 AliasMatch 上找不到 DirectoryIndex