Apache-2.2

Apache2 將 www 重定向到非 www(使用 SSL)

  • October 20, 2016

聰明人,

我正在嘗試完成以下任務:

重定向這些:

http://www.veluwsvuurbarneveld.nl    
http://veluwsvuurbarneveld.nl 
https://www.veluwsvuurbarneveld.nl

https://veluwsvuurbarneveld.nl

目標是將所有相關的 URL 發送到非 www 的 SSL 地址。

這是 mij apache2 配置中的程式碼(指向 SSL 密鑰的行已刪除):

<VirtualHost *:443>
    ServerName veluwsvuurbarneveld.nl
    DocumentRoot /var/www/

    SSLEngine on
    SSLCertificateFile /etc/ssl/certs/...
    SSLCertificateKeyFile /etc/ssl/private/...
    SSLCACertificateFile /etc/ssl/certs/...
</VirtualHost>
<VirtualHost *:80>
    ServerName www.veluwsvuurbarneveld.nl
    Redirect permanent / https://veluwsvuurbarneveld.nl/
</VirtualHost>
<VirtualHost *:80>
    ServerName veluwsvuurbarneveld.nl
    Redirect permanent / https://veluwsvuurbarneveld.nl/
</VirtualHost>
<VirtualHost *:443>
    ServerName www.veluwsvuurbarneveld.nl
    Redirect permanent / https://veluwsvuurbarneveld.nl/
</VirtualHost>

<Directory /var/www/>
   Options +ExecCGI
           Options Indexes FollowSymLinks MultiViews
           AllowOverride All
           Order allow,deny
           allow from all
</Directory>

結果是http://veluwsvuurbarneveld.nl被正確重新路由,但是兩個 www. URL 不是。我的第一個想法是https://www。還需要證書。然而,這並不能解釋其他 www。不工作。

我的 .htaccess 看起來像這樣(由 wordpress 生成):

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

apache2ctl -S 的輸出:

VirtualHost configuration:
*:80                   is a NameVirtualHost
        default server localhost (/etc/apache2/sites-enabled/000-default.conf:1)
        port 80 namevhost localhost (/etc/apache2/sites-enabled/000-default.conf:1)
        port 80 namevhost www.veluwsvuurbarneveld.nl (/etc/apache2/sites-enabled/veluwsvuurbarneveld.nl.conf:10)
        port 80 namevhost veluwsvuurbarneveld.nl (/etc/apache2/sites-enabled/veluwsvuurbarneveld.nl.conf:14)
*:443                  is a NameVirtualHost
        default server veluwsvuurbarneveld.nl (/etc/apache2/sites-enabled/veluwsvuurbarneveld.nl.conf:1)
        port 443 namevhost veluwsvuurbarneveld.nl (/etc/apache2/sites-enabled/veluwsvuurbarneveld.nl.conf:1)
        port 443 namevhost www.veluwsvuurbarneveld.nl (/etc/apache2/sites-enabled/veluwsvuurbarneveld.nl.conf:18)
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex watchdog-callback: using_defaults
Mutex rewrite-map: using_defaults
Mutex ssl-stapling: using_defaults
Mutex ssl-cache: using_defaults
Mutex default: dir="/var/lock/apache2" mechanism=fcntl
Mutex mpm-accept: using_defaults
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="www-data" id=33
Group: name="www-data" id=33

請注意,我想使用 apache2 配置文件解決這個問題,而不是通過 mod_rewrite ( httpd.apache.org/docs/trunk/rewrite/avoid.html )

有任何想法嗎?

謝謝你回答我的問題。將您的(RDA)程式碼用於 apache2 配置文件後,問題仍然存在。事實證明您的程式碼完全正確,但問題出在我的 DNS 記錄上。

我忘記將 DNS 記錄添加到 www。我的提供商(Digital Ocean)的域名。要使上述程式碼正常工作,首先需要將 www 和非 www 請求定向到我的伺服器。添加 www.veluwsvuurbarneveld.nl 的 DNS 記錄後,問題就解決了。

我的錯!

這應該有效:

<VirtualHost *:80>
    ServerName veluwsvuurbarneveld.nl
    ServerAlias www.veluwsvuurbarneveld.nl
    Redirect permanent / https://veluwsvuurbarneveld.nl/
</VirtualHost>
<VirtualHost *:443>
    ServerName www.veluwsvuurbarneveld.nl

    SSLEngine on
    SSLCertificateFile /etc/ssl/certs/...
    SSLCertificateKeyFile /etc/ssl/private/...
    SSLCACertificateFile /etc/ssl/certs/...

    Redirect permanent / https://veluwsvuurbarneveld.nl/
</VirtualHost>
<VirtualHost *:443>
    ServerName veluwsvuurbarneveld.nl
    DocumentRoot /var/www/

    SSLEngine on
    SSLCertificateFile /etc/ssl/certs/...
    SSLCertificateKeyFile /etc/ssl/private/...
    SSLCACertificateFile /etc/ssl/certs/...
</VirtualHost>

<Directory /var/www/>
   Options +ExecCGI
           Options Indexes FollowSymLinks MultiViews
           AllowOverride All
           Order allow,deny
           allow from all
</Directory>

引用自:https://serverfault.com/questions/774242