【问题标题】:apache redirect rules for multimple domains | https://www多个域的 apache 重定向规则 | https://www
【发布时间】:2014-10-16 13:44:38
【问题描述】:

我在一个 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 证书),但没有重定向规则。

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

提前致谢, 亲切的问候!

【问题讨论】:

    标签: apache mod-rewrite rewrite


    【解决方案1】:

    看起来您正在尝试将 HTTP 流量重定向到 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}
    

    【讨论】:

    • 不幸的是它没有改变任何事情。看起来每个请求都包含 www。 (所以https://www.test.net 到)被应用程序example.com 配置(?)捕获,因为https://www.test.net 重定向到https://www.test.net 但显示example.com 应用程序的内容。感谢您的建议!仍在寻找
    • 检查您的 example.com conf 并确保没有对 test.net 的引用 - 或者只是编辑您的问题并将其发布到那里。
    • 我刚刚在我的问题中添加了 example.com 的虚拟主机配置。任何建议都非常感谢! :) 到目前为止不知道我的错误在哪里。
    猜你喜欢
    • 2016-10-17
    • 2020-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-16
    • 2019-02-04
    • 1970-01-01
    • 2019-11-15
    相关资源
    最近更新 更多