: WordPress HTTP to HTTPS without trailing slash results in double redirect I have noticed that one of my WordPress powered sites is experiencing double redirects when a user or bot attempts to
I have noticed that one of my WordPress powered sites is experiencing double redirects when a user or bot attempts to visit a URL without HTTPS and a trailing slash.
In short:
example.com/contact-us redirects to www.example.com/contact-us https://www.example.com/contact-us redirects to www.example.com/contact-us/
My htaccess file looks like this:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ www.example.com/ [R=301,L]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
What do I need to change to prevent these double redirects occurring?
More posts by @Nimeshi995
1 Comments
Sorted by latest first Latest Oldest Best
I have managed to get this working by using a snippet provided by anubhava on Stack Overflow. If anyone spots a potential issue with this code or has a better and cleaner redirect then please don't hesitate to plonk your code as an answer.
SOURCE
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain.com$ [OR]
RewriteCond %{HTTPS} off
RewriteRule ^/?$ www.domain.com [R=301,L]
RewriteCond %{HTTP_HOST} ^domain.com$ [OR]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{REQUEST_URI} !(/$|.)
RewriteRule ^(.+?)/?$ www.domain.com// [R=301,L]
So my .htaccess file now looks like this:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTPS} off
RewriteRule ^/?$ www.example.com/ [R=301,L]
RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{REQUEST_URI} !(/$|.)
RewriteRule ^(.+?)/?$ www.example.com// [R=301,L]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.