apache 多个域的重写规则 | https://www

apache 多个域的重写规则 | https://www

我在一个 apache 上配置了两个应用程序(例如 example.com 和 test.net)。这些应用程序的重写规则是相同的(因此在域 example.com 的配置文件中我有这些规则,在 test.net 域的 conf 文件中也有相同的规则)

RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R]

RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI}

它几乎可以正常工作,除了一种情况 - 当我在 webwrowser 中请求时https://www.test.net
In this only case apache redirets to https://www.test.net but displays content of https://www.example.com (not content of https://www.test.net as it should). Request www.test.net works ok. And https:// www.example.com also works ok.

以下是 test.net 的整个虚拟主机配置(example.com 的配置类似):

<VirtualHost *:80>
    ServerName test.net

    DocumentRoot /var/www/test/web/
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/www/test/web/>
        Options FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/test_error.log
    LogLevel warn
    CustomLog ${APACHE_LOG_DIR}/test_access.log combined

    RewriteEngine on  

    RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
    RewriteRule ^ https://%1%{REQUEST_URI} [L,R]

    RewriteCond %{SERVER_PORT} !^443$
    RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI}


</VirtualHost>

例如.com 的虚拟主机文件

<VirtualHost *:80>
        ServerName example.com
        ServerAlias www.example.com

        DocumentRoot /var/www/example/
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/example/>
                Options FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/example_error.log
        LogLevel warn   
        CustomLog ${APACHE_LOG_DIR}/example_access.log combined

        RewriteEngine on

        RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
        RewriteRule ^ https://%1%{REQUEST_URI} [L,R]

        RewriteCond %{SERVER_PORT} !^443$
        RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI}

</VirtualHost>

我当然也有用于 SSL 设置的配置文件(例如 SSL 证书),但没有重定向规则。

如何为这两个应用程序配置重写规则以使其正常工作?
任何建议都非常感谢!到目前为止,我不知道我的错误在哪里。

提前致谢,
诚挚问候!

答案1

我认为您的 VirtualHost 配置中缺少某些内容ServerAlias www.test.com,这导致了这些问题。ServerAlias www.example.com

相关内容