Mobile app version of vmapp.org
Login or Join
Nimeshi995

: 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

@Nimeshi995

Posted in: #Apache #Htaccess #Https #ModRewrite #Wordpress

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?

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Nimeshi995

1 Comments

Sorted by latest first Latest Oldest Best

 

@Radia820

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>

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme