如何使用 htaccess 重定向除一个子目录之外的所有子目录
自从我搞乱 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
也许是这个? (需要mod_rewrite)
Maybe this? (needs mod_rewrite)
这对我来说在 Apache 2.2.13 上工作得很好,我在其中重新创建了您描述的目录结构。
括号中的模式表示如果 URI 包含字符串“special”,则不匹配。
此外,mod 重写并不总是可用,在这种情况下不需要。
This works fine for me on Apache 2.2.13 where I recreated the directory structure you described.
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.
尝试这个:
Try this:
或许...?
Maybe...?