【问题标题】:htaccess show the same content of main domain, when you are on subdomain当您在子域上时,htaccess 显示主域的相同内容
【发布时间】:2015-09-30 12:29:15
【问题描述】:

我有网站www.domain.com(服务器根目录位于/domain.com/web/),现在我创建了子域m.domain.com(服务器根目录位于/domain.com/sub/m/)。

我将 .htaccess 添加到子域根目录:

RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} ^(www\.)?m\.domain\.com$
RewriteCond %{REQUEST_URI} !^/m
RewriteRule ^(.*)$ ../../web/$1 [QSA,L]

但它显示的是Internal Server Error

我试过了:

RewriteRule ^(.*)$ http://www.domain.com/$1 [QSA,L]

但这会将您重定向到主域。我需要留在子域,但内容与主域相同。

我也在这里查看了更多关于 SO 的解决方案,但没有一个有效。

编辑:

我尝试在 Apache 中设置另一个 VirtualHost:

DocumentRoot /domain.com/web
<Directory "/domain.com/web">
allow from all
Options None
</Directory>
ServerName m.domain.com
ServerAlias m.domain.com

现在主域和子域也显示 503 - Forbidden。

【问题讨论】:

  • 你检查了你的 apache 的错误日志?
  • 通常,错误显示在error.log 而不是access.log。您的 access.log 条目确认存在“内部服务器错误”(500)。
  • 如果您希望两个域上的内容相同,为什么要使用单独的DocumentRoots?您可以将两个根都设置为/domain.com/web/
  • error_log 已禁用。现在显示的重定向太多了,所以它被循环不定式 - Request exceeded the limit of 10 internal redirects due to probable configuration error.
  • 搜索重定向循环和环境变量REDIRECT_STATUS,例如stackoverflow.com/q/13008782/1741542

标签: .htaccess mod-rewrite redirect subdomain


【解决方案1】:

我不知道为什么这必须过于复杂。如果您想使用相同的内容,那么我可以看到一些方法来做到这一点。

  1. 刚刚将m.domain.com 添加到Apache VirtualHost 配置中的ServerAlias 指令中,对该子域的所有请求都将显示设置的文档根目录中的内容,但保持子域显示。 (最推荐)
  2. 将子域配置文件的document root 设置为与主域名相同。 (不太推荐)
  3. 通过P 标志使用mod_proxy 对内容进行反向代理,以便仍显示子域。 (最不推荐)

通过P 标志使用mod_proxy 的示例。

RewriteCond %{HTTP_HOST} ^m\.domain\.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [P]

旁注:m.domain.com 也必须在指向您的服务器 IP 的 DNS 中定义。

您的主域的虚拟主机应如下所示。您的m 子域不需要虚拟主机。

<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias m.example.com 
    DocumentRoot "/www/domain.com/web"
    <Directory "/www/domain.com/web">
       # deny .htaccess use.
       AllowOverride None
       Order allow,deny
       Allow from all
    </Directory>
</VirtualHost>

重新启动 Apache 以进行所有配置更改。

【讨论】:

  • *.domain.com 在 DNS 中定义并指向同一个服务器 IP。但我想使用你的第一个解决方案,但不知道如何。
  • @Legionar 单击指向 ServerAlias 的链接。它解释了它是如何工作的。
  • 你用的是什么版本的apache?
  • 我修好了这条线。您是否删除了任何重定向?同时清除您的浏览器缓存。
  • 非常感谢!我还需要在那里添加ServerAlias domain.com,在没有www 的情况下也可以访问主域。
猜你喜欢
  • 2016-02-06
  • 2014-02-01
  • 1970-01-01
  • 2014-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多