使用 .htaccess 仅将根目录的流量重定向到特定页面

发布于 2024-09-19 18:27:36 字数 215 浏览 3 评论 0原文

我设置了一个包含索引等的网站。我只想将直接访问 www.example.com 的流量重定向到 www.example.com/foo.php.但是,如果用户尝试直接访问 www.example.com/index.php 或任何其他现有页面,则请求应正常返回适当的页面。我似乎无法理解这些重写规则。我如何使用 .htaccess 来实现这一点?

I have a site set up complete with an index, etc. I would like to redirect only traffic coming directly to www.example.com to www.example.com/foo.php. However, if a user tries to access www.example.com/index.php or any of the other existing pages directly, the request should return the appropriate page as normal. I can't seem to wrap my head around these rewrite rules. How would I accomplish this with .htaccess?

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

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

发布评论

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

评论(1

玩物 2024-09-26 18:27:36

您可能只需更改 DirectoryIndex/foo.php

DirectoryIndex foo.php

但是,如果您想使用 mod_rewrite 来实际重写 URL,有几种不同的方法可以完成此操作,但最清晰的方法是仅检查请求的资源是否为空字符串(已请求根目录,并且 .htaccess 文件中 RewriteRule 的输入不包含前导斜杠):

RewriteEngine On

RewriteRule ^$ /foo.php

对服务器的任何不针对 的请求/ 将被 Apache 正常忽略和路由。

You could likely get away with just changing the DirectoryIndex to /foo.php:

DirectoryIndex foo.php

However, if you wanted to use mod_rewrite to actually rewrite the URL, there are a few different ways to accomplish this, but the clearist is to just check if the requested resource is an empty string (the root was requested, and the input to RewriteRule in .htaccess files doesn't contain a leading slash):

RewriteEngine On

RewriteRule ^$ /foo.php

Any request to the server that is not for / will simply be ignored and routed by Apache as normal.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文