【问题标题】:Apache2 rewrite rules not being picked upApache2重写规则没有被拾取
【发布时间】:2016-04-25 00:12:05
【问题描述】:

我有一个使用 2 个单独的虚拟主机文件的域:一个用于 :80,一个用于 :443

:80 设置非常简单,唯一的工作就是重定向到 :443:

<VirtualHost *:80>
    # This is the first host so it's the default.
    # So although I've specified a ServerName and ServerAlias anything else not specified elsewhere will also end up here.
    ServerName www.domain.com
    ServerAlias domain.com

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # Redirect everything to https:
    RewriteEngine on
    RewriteRule ^(.*)$ https://www.domain.com$1 [R=301,L]
</VirtualHost>

如果 :443 不存在,只需将 www 添加到 url 的开头即可:

<VirtualHost *:443>
    # This is the first host so it's the default.
    # So although I've specified a ServerName and ServerAlias anything else not specified elsewhere will also end up here.
    ServerName www.domain.com
    ServerAlias domain.com

    ErrorLog ${APACHE_LOG_DIR}/ssl.error.log
    CustomLog ${APACHE_LOG_DIR}/ssl.access.log combined

    # Redirect everything which is not already on the real www domain name to that:
    RewriteEngine on
    RewriteCond %{HTTP_HOST} !www.domain.com
    RewriteRule ^(.*)$ https://www.domain.com$1 [R=301]

    ErrorDocument 404 /404.html
</VirtualHost>

我有 1 个案例,其中这些重写似乎失败了:

https://domain.com -> 应该指向 https://www.domain.com 但它指向 https://www.domain.com%24/# 。显然后面的字符会阻止 DNS 服务器找到域。

是什么导致了这个问题?我已经帮助创建了这些虚拟主机文件,但它们似乎仍未按预期完全工作。

但我也想将我的 URL 重写为更好的 URL。我认为我的规则是正确的,并且 :443 中的 Rewrite 块如下所示

RewriteEngine on
RewriteCond %{HTTP_HOST} !www.domain.com
RewriteRule ^(.*)$ https://www.domain.com$1 [R=301]

RewriteRule ^subpage/(.+)/?$ subpage.html?$1 [NC]

应该改写的

https://www.domain.com/subpage/2 -> https://www.domain.com/subpage.html?2 但它现在只是指向我的 404 文件。

这可能是显而易见的,但我没有看到我的错误。

【问题讨论】:

    标签: mod-rewrite apache2


    【解决方案1】:

    注意:这并不能解决此处所述的问题,但确实解决了根本问题。

    因为这是一个生产环境(多么尴尬)。我的网络服务器一开始就很弱,很快就被淹没了。我的公司大大增加了我的预算(我们预计会有很多流量,但希望它会慢慢增加,但事实并非如此),所以我能够设置多个服务器并在它们前面放置 2 个 HAProxy 负载均衡器。我使用 HAProxy 配置来解决我的问题:

    frontend http
        bind MY_IP:80
        redirect prefix http://www.domain.com code 301 if { hdr(host) -i domain.com }
        redirect scheme https code 301 if !{ ssl_fc }
    
    frontend https
        bind MY_IP:443 ssl crt /etc/haproxy/certs/domain.com.pem
        redirect prefix https://www.domain.com code 301 if { hdr(host) -i domain.com }
        reqadd X-Forwarded-Proto:\ https
        default_backend app_pool
    
    backend app_pool
        balance roundrobin
        redirect scheme https if !{ ssl_fc }
        server app-1 MY_IP:80 check
        server app-2 MY_IP:80 check
        server app-3 MY_IP:80 check
        server app-4 MY_IP:80 check
    

    它将始终重定向到 www.domain.com 版本并强制执行 HTTPS。在我看来,在 HAProxy 中进行设置比使用 VirtualHost 更容易。

    【讨论】:

      猜你喜欢
      • 2012-01-12
      • 1970-01-01
      • 2013-07-12
      • 2011-11-17
      • 2011-11-25
      • 1970-01-01
      • 2015-07-10
      • 2014-08-10
      • 1970-01-01
      相关资源
      最近更新 更多