如何使用 mod_rewrite 在内部将 / 重写为 /homepage

发布于 2024-09-07 03:06:07 字数 362 浏览 0 评论 0原文

好吧,这应该很简单,但我遇到了很多麻烦。基本上,我试图将 http://server.com/ 重写为 http://server.com/homepage,但我只希望它在内部重写--用户永远不应该看到 /homepage URL。我尝试了这个:

RewriteRule ^$ /homepage [L]

它正确匹配 URL,但它发出 301 重定向,而不是仅在内部处理它 - 用户被重定向到 http://server.com/homepage。我该如何改变这一点?

我觉得这很简单,我完全错过了它,所以希望有人能帮忙!

Ok, this should be simple, but I'm having a fair amount of trouble with it. Basically, I'm trying to rewrite http://server.com/ to http://server.com/homepage, but I only want it to rewrite it internally--the user should never see the /homepage URL. I tried this:

RewriteRule ^$ /homepage [L]

And it properly matches the URL, but it issues a 301 redirect instead of just handling it internally--the user is redirected to http://server.com/homepage. How do I change that?

I feel like this is something pretty simple, and I'm completely missing it, so hopefully someone out there can help!

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

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

发布评论

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

评论(1

陌路终见情 2024-09-14 03:06:08

假设 homepage 是一个目录,请尝试以下操作:

RewriteRule ^$ /homepage/

我完全忘记了这里的内容,所以希望这能解决问题(它对我有用),然后我将用解释更新我的答案为了完整起见,正在发生的事情。

编辑:啊,对了,它是mod_dir< /code>及其 DirectorySlash 选项尝试清理 URL 丢失的尾部斜杠。如果尾部没有斜杠,则不会调用 DirectoryIndex 处理程序,因为 mod_rewrite 只是在此处理发生之前重写 URL 默认情况下,DirectorySlash 设置为 Onmod_dir 执行重定向到斜杠完成的 URL。

Assuming homepage is a directory, try this instead:

RewriteRule ^$ /homepage/

I forget exactly what's at play here, so hopefully that'll fix the problem (it works for me), then I'll update my answer with an explanation of what's going on, for the sake of completeness.

Edit: Ah right, it's mod_dir and its DirectorySlash option trying to clean up the URL's missing trailing slash. If you don't have the trailing slash, the DirectoryIndex handler isn't invoked, and since mod_rewrite just rewrites the URL before this processing happens and DirectorySlash is, by default, set to On, mod_dir performs a redirect to the slash-completed URL.

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