apache - 重定向错误 : Wordpress + Let's Encrypt (Certbot) + SSL only + non-www

标签 apache ssl mod-rewrite https url-rewriting

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

我的 apache 配置文件配置不当,现在出现重定向错误 (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 中的 Redirect permanent/https://d0main.xyz 解决了重定向错误。 conf,但现在 URL 末尾缺少一个 /。例如,图形 URL 现在是 https://d0main.xyzwp-content/image.jpg

更新 2:这变得越来越奇怪了。我在我的 d0main.xyz 中将行 Redirect permanent/https://d0main.xyz 更改为 Redirect permanent/https://d0main.xyz\/。 session 。现在一些图像有两个斜线(并且有效)https://d0main.xyz//wp-content/uploads/2016/10/logo-5.png,而其他图像仍然没有斜线他们的网址: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:我的最终(工作)conf 文件:

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>

最佳答案

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

代替下面几行。

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

你应该有一个用于 www.d0main.xyz 的 SSL 虚拟主机,然后执行 Redirect/https://d0main.xyz/

如果您仍然看到双斜线,则原因可能出在其他地方。你有任何 htaccess 文件吗?

关于apache - 重定向错误 : Wordpress + Let's Encrypt (Certbot) + SSL only + non-www,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39948113/

相关文章:

使用简单文件上传脚本时出现PHP错误: failed to open stream: No such file or directory

c - 使用正则表达式解析 URL 参数

php - 在 PHP 中运行 inkscape

c++ - 在 windows 中通过 libcurl 通过 ssl 传输 ftp

amazon-web-services - Elastic Beanstalk 不创建 ssl.conf

php - 如何通过 django 和 php 在同一域上提供动态内容?

ssl - 无法访问证书存储

.htaccess - 使用 Htaccess 和 Mod_Rewrite 重写 SEO 友好 URL

php - PHP 中的基本 URL 重写问题 (mod_rewrite)

regex - .htaccess 重定向到子目录