【问题标题】:How can I internally point to a domain but keep the url with the subdomain constant?我如何在内部指向一个域,但保持子域的 url 不变?
【发布时间】:2013-09-28 00:33:56
【问题描述】:

我在一个位置托管了一个域 www.example.com。

我使用不同的托管服务提供商创建了另一个帐户,允许我创建子域:www.test1.example.com 和 www.test2.example.com

当用户访问 test1.example.com、test2.example.com 时,我为每个子域放置一个自定义页面......

用户登录此自定义页面后,我想维护子域(test1.example.com),但内部所有请求都指向 www.example.com。

我在 Apache 上运行自定义页面,在 Apache Tomcat 上运行域页面 - 我认为使用“mod_rewrite”是要走的路?

【问题讨论】:

    标签: apache subdomain


    【解决方案1】:

    所以,基本上我发现有效的答案是使用 mod_proxy。我将其作为 Apache 模块启用,并在我的 httpd-vhosts.conf 文件中包含以下内容。

    NameVirtualHost *:80
    
    <VirtualHost *:80>
        ServerName test1.example.com
        DocumentRoot "location_of_the_custom_page"
        ErrorLog "logs\errors.log"
        <directory "D:\wamp\www\capitalfloat">
            Options Indexes FollowSymLinks
            AllowOverride all
            Order Deny,Allow
            Deny from all
        Allow from all
        </directory>
    </VirtualHost>
    
    <VirtualHost *:80>
        ServerName localhost2
        ServerAlias *.example.com
        ErrorLog "logs\errors.log"
    
        ProxyRequests Off
        ProxyPreserveHost On
    
        <Proxy *>
            Order deny,allow
            Allow from all
        </Proxy>
    
        ProxyPass / http://www.example.com
        ProxyPassReverse / http://www.example.com
    </VirtualHost>
    

    我还必须在 Windows 主机文件中包含“http://www.example.com”和“test1.example.com”(对我来说,C:\Windows\System32\drivers\etc\hosts)。在我的自定义登录页面中,请求转到“example.com”,所有后续请求都发送到“www.example.com”,但网址仍然显示“test1.example.com/...”

    【讨论】:

      【解决方案2】:

      将以下内容放入子域文档根目录的 .htaccess 文件中:

      RewriteEngine On
      RewriteCond %{HTTP_HOST} ^test1\.example\.com$
      RewriteRule ^/(.*) http://example.com/$1 [redirect,last]
      

      要在现代 Ubuntu Web 服务器上启用 mod_rewrite,请运行以下命令:

      sudo ln -s /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled/rewrite.load
      

      确保在您的 VirtualHost 配置中将 AllowOverride 设置为“全部”(例如 /etc/apache2/sites-available/default):

      <Directory /var/www/document/root/>
          AllowOverride All
      </Directory>
      

      然后重启 Apache:

      sudo /etc/init.d/apache2 restart
      

      【讨论】:

      • 更多信息:我在 Windows azure 虚拟环境中运行主要的“www.example.com”。此外,您的建议不是“重定向”页面(包括更改 URL)。我想保持相同的 URL。
      猜你喜欢
      • 2016-03-05
      • 2015-09-05
      • 2021-04-19
      • 2012-05-05
      • 1970-01-01
      • 1970-01-01
      • 2017-01-26
      • 1970-01-01
      • 2018-02-19
      相关资源
      最近更新 更多