如何使用 mod_rewrite 在内部将 / 重写为 /homepage
好吧,这应该很简单,但我遇到了很多麻烦。基本上,我试图将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设
homepage
是一个目录,请尝试以下操作:我完全忘记了这里的内容,所以希望这能解决问题(它对我有用),然后我将用解释更新我的答案为了完整起见,正在发生的事情。
编辑:啊,对了,它是
mod_dir< /code>
及其
DirectorySlash
选项尝试清理 URL 丢失的尾部斜杠。如果尾部没有斜杠,则不会调用DirectoryIndex
处理程序,因为mod_rewrite
只是在此处理发生之前重写 URL 和 默认情况下,DirectorySlash
设置为On
,mod_dir
执行重定向到斜杠完成的 URL。Assuming
homepage
is a directory, try this instead: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 itsDirectorySlash
option trying to clean up the URL's missing trailing slash. If you don't have the trailing slash, theDirectoryIndex
handler isn't invoked, and sincemod_rewrite
just rewrites the URL before this processing happens andDirectorySlash
is, by default, set toOn
,mod_dir
performs a redirect to the slash-completed URL.