Forcing HTTPS is removing the trailing slash on domain and causing incorrect URL

February 6, 2019 962 views
Ubuntu 18.04 Configuration Management PHP Apache WordPress

Ubuntu 18.04, Apache, php7.2, Wordpress (all plugins deactivated)

Can’t figure this out. Trying to force all to https://www but here is the problem. If I enter the following domain, it loads.

https://www.example.com/post/

However, if I remove the ’s’ in the domain

http://www.example.com/post/

it reverts to this. Notice the trailing slash is gone after the .com, as well as the trailing slash at end of URL.

https://www.example.compost

from the very top of my 000-default.conf

<VirtualHost *:80>
   ServerName example.com
   Redirect permanent / https://www.example.com/
</VirtualHost>

my htaccess

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^44\.66\.7\.278$
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
</IfModule>

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

Options -Indexes
1 Answer

Hi!

You should try this :

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

RewriteCond %{HTTP_HOST} ^yourwebsite.com$
RewriteRule ^(.*) https://www.yourwebsite.com/$1 [QSA,L,R=301]

You must also define in the Wordpress settings your favorite url

Have another answer? Share your knowledge.