【问题标题】:Use a different directory for the homepage without switching DocumentRoot在不切换 DocumentRoot 的情况下为主页使用不同的目录
【发布时间】:2019-02-15 12:17:24
【问题描述】:

我正在从 Wordpress 博客切换到 Hugo 博客。我想在 example.com 上切换内容以显示来自 /var/www/new-blog 的内容。它目前只显示 /var/www/ 中的内容。我想将它保存在 new-blog 目录中,并且仍然是我网站根目录中显示的内容。

但我不想更改 DocumentRoot,因为我不想影响 /var/www/ 中的其他目录。

我需要使用 mod_rewrite 来完成这个吗?

【问题讨论】:

  • @Nic3500 我似乎无法让它工作。 Alias "/" "/new-blog" 之类的东西不应该是这样吗?
  • @Nic3500 我做了一个别名Alias "/" "/var/www/new-blog",而不是在根目录显示内容,而是在新博客中列出内容。但是,直接访问 example.com/new-blog 可以正确地提供页面。
  • @Nic3500 错误日志指出我正在达到最大重定向数量。

标签: apache mod-rewrite


【解决方案1】:

这是我从你的帖子中了解到的。

现状

目录结构:

1) /var/wwww/
2) /var/www/new-blog/

1) DocumentRoot /var/www/
所以http://example.com/ 指向/var/www

2) 可通过http://example.com/new-blog访问


你想要什么


解决方案

乍一看,我认为Alias 会成功,但在我自己尝试之后就不行了。

所以使用 mod_rewrite(确保 mod_rewrite 已加载)。

RewriteEngine on
RewriteRule ^/$ http://example.com/new-blog [R,L]

这样,http://example.comhttp://example.com/ 都会重定向到 new-blog 目录。

任何其他请求(即http://example.com/dir)都会回复来自/var/www/dir的内容。

需要注意的是,用户会看到他们的地址栏从 http://example.com/ 变为 http://example.com/new-blog


额外的解决方案

如果您不希望您的用户知道他们正在被重定向,您可以改为代理。

首先确保 mod_proxymod_proxy_http 已加载。去掉之前添加的 RewriteRule,改成这个配置:

ProxyPassMatch   ^/$ http://example.com/new-blog
ProxyPassReverse ^/$ http://example.com/new-blog

很好的是,这两种解决方案都可以让您访问 /var/www 下的文件或目录。只有http://example.com/ 被重定向或代理。

【讨论】:

  • 谢谢,这是对我问题的一个很好的回答。你正确理解了我的问题。我选择了 mod_rewrite 选项只是因为单击页面上的任何链接都意味着您最终会到达 /new-blog 链接。我将尝试通过使用/ 作为目录重新生成我的静态站点来解决此问题,并查看是否可以修复它以允许 mod_proxy 正常工作:)。
猜你喜欢
  • 2021-10-18
  • 1970-01-01
  • 1970-01-01
  • 2020-04-20
  • 1970-01-01
  • 2010-12-21
  • 1970-01-01
  • 1970-01-01
  • 2012-11-18
相关资源
最近更新 更多