【问题标题】:Redirect Error: Wordpress + Let's Encrypt (Certbot) + SSL only + non-www重定向错误:Wordpress + Let's Encrypt (Certbot) + SSL only + non-www
【发布时间】:2017-02-18 06:36:24
【问题描述】:

注意:请参阅本文末尾的更新。对于最终(工作)配置文件,请参阅本文末尾的更新 4 或我标记为解决方案的帖子。

我的 apache conf 文件配置错误,现在出现重定向错误 (ERR_TOO_MANY_REDIRECTS)。我想将所有内容重定向到 HTTPS(非 www)。我已经尝试将其添加到有关this tips 的 wp-config.php 中,但这并没有解决问题:

define('WP_HOME','http://d0main.xyz');
define('WP_SITEURL','http://d0main.xyz');

我尝试添加

define('WP_HOME','https://d0main.xyz');
define('WP_SITEURL','https://d0main.xyz');

这是我的 Apache 文件:

d0main.xyz.conf

<VirtualHost *:80>

ServerName d0main.xyz
ServerAlias www.d0main.xyz
ServerAdmin contact@d0main.xyz
DocumentRoot /var/www/html
Redirect permanent / https://d0main.xyz

<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
# changed from None to FileInfo
AllowOverride FileInfo
Order allow,deny
allow from all
</Directory>

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

d0main.xyz-le-ssl.conf

<IfModule mod_ssl.c>
<VirtualHost *:443>

ServerName d0main.xyz
ServerAlias www.d0main.xyz
ServerAdmin d0main@d0main.xyz
DocumentRoot /var/www/html
Redirect permanent / https://d0main.xyz

<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
# changed from None to FileInfo
AllowOverride FileInfo
Order allow,deny
allow from all
</Directory>

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

RewriteEngine on
SSLCertificateFile /etc/letsencrypt/live/d0main.xyz/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/d0main.xyz/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]
</VirtualHost>

</IfModule>
<IfModule mod_rewrite.c>

更新 1:我通过删除 d0main.xyz-le-ssl.conf 中的 Redirect permanent / https://d0main.xyz 解决了重定向错误,但现在 URL 末尾缺少 /。例如,图形 URL 现在是 https://d0main.xyzwp-content/image.jpg

更新 2:这越来越奇怪了。我将d0main.xyz.conf 中的Redirect permanent / https://d0main.xyz 行更改为Redirect permanent / https://d0main.xyz\/。现在有些图片有两个斜线(并且有效)https://d0main.xyz//wp-content/uploads/2016/10/logo-5.png,而其他图片的 URL 中仍然没有斜线:https://d0main.xyzwp-content/uploads/2016/10/image2.png

更新 3:我忘记发布我的 .htaccess

# 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

更新 4:我的最终(工作)配置文件:

d0main.xyz.conf

<VirtualHost *:80>
ServerName d0main.xyz
ServerAlias www.d0main.xyz
ServerAdmin contact@d0main.xyz
DocumentRoot /var/www/html
Redirect permanent / https://d0main.xyz/

<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
# changed from None to FileInfo
AllowOverride FileInfo
Order allow,deny
allow from all
</Directory>

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

d0main.xyz-le-ssl.conf

<IfModule mod_ssl.c>
<VirtualHost *:443>

ServerName delegatex.xyz
ServerAlias www.delegatex.xyz
ServerAdmin delegatexyz@gmail.com
DocumentRoot /var/www/html

<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
# changed from None to FileInfo
AllowOverride FileInfo
Order allow,deny
allow from all
</Directory>

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

RewriteEngine on
SSLCertificateFile /etc/letsencrypt/live/delegatex.xyz/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/delegatex.xyz/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf

</VirtualHost>

</IfModule>
<IfModule mod_rewrite.c>

【问题讨论】:

    标签: apache ssl mod-rewrite https url-rewriting


    【解决方案1】:

    您不需要转义正斜杠,因此您应该只使用Redirect permanent / https://d0main.xyz/ 而不是Redirect permanent / https://d0main.xyz\/。但是在重定向(和 ProxyPassing)时,您应该始终匹配尾部斜杠。

    而不是以下几行。

    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]
    

    你应该有一个用于 www.d0main.xyz 的 SSL 虚拟主机,并且只需要 Redirect / https://d0main.xyz/

    如果您仍然收到双斜线,则原因似乎必须在其他地方。你有任何 htaccess 文件吗?

    【讨论】:

    • 是的,我有一个 .htaccess 可以删除 Wordpress 永久链接中的 index.php。这是它的外观:# BEGIN WordPress &lt;IfModule mod_rewrite.c&gt; RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] &lt;/IfModule&gt; # END WordPress 将在我的第一篇文章中将其发布为 Update 3
    • 好的,看起来它不会改变任何东西。因此,只需尝试从重定向中删除反斜杠。看看能不能解决问题。只需确保您的任何重定向在第一个和第二个参数上都有匹配的尾部斜杠,
    • 谢谢。现在可以了。将在 Update 4 中添加我的最终 Apache 配置文件。
    猜你喜欢
    • 2020-03-18
    • 2017-11-10
    • 2020-03-11
    • 2016-09-06
    • 1970-01-01
    • 2017-10-24
    • 2012-06-09
    • 2021-06-20
    相关资源
    最近更新 更多