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

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

明智的人,

我正在尝试完成以下任务:

重定向这些:

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_rewritehttpd.apache.org/docs/trunk/rewrite/avoid.html

有任何想法吗?

答案1

这应该有效:

<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>

答案2

感谢您回答我的问题。使用您的(RDA)代码编写 apache2 配置文件后,问题仍然存在。事实证明您的代码完全正确,但问题出在我的 DNS 记录上。

我忘记向我的提供商 (Digital Ocean) 添加 www. 域的 DNS 记录。要使上述代码正常工作,www 和非 www 请求首先都需要定向到我的服务器。添加 www.veluwsvuurbarneveld.nl 的 DNS 记录后,问题就解决了。

我的错!

相关内容