2016-03-23 41 views
0

我有它使用2個獨立的虛擬主機文件的域:一爲:80,一個用於:443的Apache2重寫規則沒有被拾起

的: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到更好的。我想,我的原則是正確的,並在重寫塊:443看起​​來如下

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文件現在。

這可能是明顯的,但我沒有看到我的錯誤。

回答

0

注意:這並不解決這裏所述的問題,但它確實解決了根本問題。因爲這是一個生產環境(多麼尷尬)。我的網絡服務器,開始時相當弱,很快被洪水淹沒。我的公司大大增加了我的預算(我們預計會有很多流量,但希望能夠慢慢建立起來,所以我可以設置多臺服務器並在它們前面放置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更容易。