使用 .htaccess 仅将根目录的流量重定向到特定页面
我设置了一个包含索引等的网站。我只想将直接访问 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能只需更改
DirectoryIndex
到/foo.php
:但是,如果您想使用
mod_rewrite
来实际重写 URL,有几种不同的方法可以完成此操作,但最清晰的方法是仅检查请求的资源是否为空字符串(已请求根目录,并且.htaccess
文件中RewriteRule
的输入不包含前导斜杠):对服务器的任何不针对
的请求/
将被 Apache 正常忽略和路由。You could likely get away with just changing the
DirectoryIndex
to/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 toRewriteRule
in.htaccess
files doesn't contain a leading slash):Any request to the server that is not for
/
will simply be ignored and routed by Apache as normal.