多个域的apache重定向规则| https://开头WWW

时间:2014-10-16 13:44:39

标签: apache mod-rewrite rewrite

我在一个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中请求时,它几乎可以正常工作。 在这种情况下,apache重定向到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
    ServerAlias www.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>

和example.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 certs),但是没有重定向规则。

如何为这两个应用程序配置重写规则以使其正常工作?

提前致谢,  亲切的问候!

1 个答案:

答案 0 :(得分:0)

您似乎正在尝试将流量重定向到HTTPS;我假设这是正确的吗?

如果是,请尝试:

删除它:

RewriteEngine on  

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

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

并将其替换为:

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}