【问题标题】:Using fall-through rewrite for mod_vhost_alias对 mod_vhost_alias 使用贯穿重写
【发布时间】:2014-05-24 22:41:00
【问题描述】:

我在我的测试 VPS 上运行了几十个站点,目前使用 mod_vhost_alias 来避免每次我抛出一个新站点时都需要一个新的 VirtualHost。我当前的配置如下所示:

<VirtualHost *:80>
  ServerName my.servername.com
  ServerAlias *
  VirtualDocumentRoot /var/www/%0/public_html
</VirtualHost>

在我的 /var/www 目录中,每个站点都有自己的目录。例如,我的个人页面的路径是 /var/www/personalsite.com/public_html/index.php。这对http://personalsite.com 的请求非常有用。

但是,当请求进入 http://www.personalsite.com 时,这不起作用。对于我的其他一些站点,我遇到了相反的问题——目录可能是/var/www/www.sitename.com/public_html,所以对http://www.sitename.com 的请求很好。但是,http://sitename.com 的请求不起作用。

有没有办法设置我的 Apache 配置,以便在收到请求时执行以下操作?这样做对性能有影响吗?

在伪代码中:

1. Check if the directory or file exists. If it does, skip the rest of the rules 
       (but don't stop, in case a local .htaccess has rules in it for pretty URLs 
       in WordPress or Concrete5)
2. If the file/directory does not exist:
    1. If the host header starts with "www":
        1. remove the www from the host header and try the first rule again.
    2. If the host header does not start with "www":
        1. add "www" to the beginning  of the host header and try the first rule again
    3. If it still fails after trying both conditions:
        1. Go to a 404 error page

我目前正在使用大约 20 个虚拟主机执行此操作,但是当我必须为每个站点添加一个新主机时,这似乎很荒谬。使用 mod_vhost_alias 的目的是首先避免需要所有这些虚拟主机。

【问题讨论】:

    标签: apache mod-rewrite mod-vhost-alias


    【解决方案1】:

    假设您可以重定向用户,您可以使用https://stackoverflow.com/a/2361508/881615 中的一种技术:

    • 要么设置mod_rewrite 规则以删除前导www。来自请求,
    • 或在每个虚拟主机中设置重定向以重定向带有前导 www 的请求。到顶级域

    【讨论】:

    • 感谢您的回答,但我认为这并不能完全解决我想要做的事情。我的目标之一是每个站点都没有虚拟主机。我已经用伪代码重写了我正在尝试做的事情,并更新了问题。这更有意义吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多