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

发布于 2024-07-21 07:34:55 字数 564 浏览 8 评论 0原文

自从我搞乱 .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 不应重定向

I本来以为类似的东西

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

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

It's been a while since I've messed with .htaccess and I can't seem to get this quite right. I have a site, say example.com, where I want example.com/* to redirect to example.com/collector.html except for URLs under the subdirectory example.com/special, which I want to continue working.

For example:

  • example.com/page.html should redirect to example.com/collector.html
  • example.com/foo/bar should redirect to example.com/collector.html
  • example.com/special/page.html should not redirect

I would have thought that something like

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

would do the trick, but it doesn't seem to work the way I want it to.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

南城追梦 2024-07-28 07:34:55

也许是这个? (需要mod_rewrite)

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

Maybe this? (needs mod_rewrite)

RewriteEngine On
RewriteRule !^(special(/.*)?|collector\.html)$ collector.html [R,L]
堇色安年 2024-07-28 07:34:55

这对我来说在 Apache 2.2.13 上工作得很好,我在其中重新创建了您描述的目录结构。

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

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

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

This works fine for me on Apache 2.2.13 where I recreated the directory structure you described.

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

The pattern in the brackets is saying do NOT match if the URI contains the string 'special'.

Also mod rewrite is not always available and in this case is not needed.

嘿咻 2024-07-28 07:34:55

尝试这个:

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

Try this:

RedirectMatch 302 /\/[^(special)]\/.+/ /collector.html 
RedirectMatch 302 /\/[^(collector\.html)]/ /collector.html
纸短情长 2024-07-28 07:34:55

或许...?

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

Maybe...?

RewriteEngine on
RewriteRule ^(?!special) collector.html [R,NC]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文