John Smith
John Smith

Reputation: 363

Redirect Error: Wordpress + Let's Encrypt (Certbot) + SSL only + non-www

Note: See the updates at the end of this post. For the final (working) conf-files see update 4 at the end of this post or the post which I marked as the solution.

I badly configured my apache conf-files and now I'm getting a redirect error (ERR_TOO_MANY_REDIRECTS). I want to redirect everything to HTTPS (non-www). I already tried to add this to the wp-config.php regarding this tips, but that didn't solve the problem:

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

and I tried to add

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

Here are my Apache files:

d0main.xyz.conf

<VirtualHost *:80>

ServerName d0main.xyz
ServerAlias www.d0main.xyz
ServerAdmin [email protected]
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 [email protected]
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>

Update 1: I solved the redirection error by removing Redirect permanent / https://d0main.xyz in my d0main.xyz-le-ssl.conf, but now there is a / missing at the end of the URL. Graphic URL's for example are now https://d0main.xyzwp-content/image.jpg

Update 2: This is getting more weird. I changed the line Redirect permanent / https://d0main.xyz to Redirect permanent / https://d0main.xyz\/ in my d0main.xyz.conf. Now some images have two slashes (and work) https://d0main.xyz//wp-content/uploads/2016/10/logo-5.png, while other images still have no slash in their URL: https://d0main.xyzwp-content/uploads/2016/10/image2.png

Update 3: I forgot to post my .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

Update 4: My final (working) conf-files:

d0main.xyz.conf

<VirtualHost *:80>
ServerName d0main.xyz
ServerAlias www.d0main.xyz
ServerAdmin [email protected]
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 [email protected]
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>

Upvotes: 1

Views: 2670

Answers (1)

Unbeliever
Unbeliever

Reputation: 336

You do not need to escape the forward slash, so instead of Redirect permanent / https://d0main.xyz\/ you should just have Redirect permanent / https://d0main.xyz/. But when redirecting (and ProxyPassing) you should always match trailing slashes.

Instead of the following lines.

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

You should have a SSL vhost for www.d0main.xyz and jsut do Redirect / https://d0main.xyz/

If you are still getting the double slashes it looks like the reason must be elsewhere. DO you have any htaccess files?

Upvotes: 2

Related Questions