5

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:

  • http://example.com/contact-us redirects to https://www.example.com/contact-us
  • https://www.example.com/contact-us redirects to https://www.example.com/contact-us/

My htaccess file looks like this:

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.example.com/$1 [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?

  • I'm glad you found a solution that works for you, but there is generally no need to prevent double redirects. All browsers and crawlers follow chains of redirects that are reasonable length. Unless you are finding situations where you have 10 redirects chained together, I wouldn't expect it to cause any problems whatsoever either in terms of users seeing error messages, or SEO. – Stephen Ostermiller Feb 16 '17 at 13:31
  • Hehe, yep very true, I'm trying hide the notifications I get when using a spidering tool, it finds broken links for me but doesn't have a option to hide warnings regarding this non-issue. Maybe I'll look for a newer app :) – Simon Hayter Feb 16 '17 at 13:45
  • Or fix your links so they link to the final destination rather than something that redirects. If the spider can't find it, it can't complain about it. – Stephen Ostermiller Feb 16 '17 at 13:49
  • Certainly, if these are internal links then the links should be fixed first, rather than masking the problem with a redirect. The redirect would be for any stray inbound links. – MrWhite Feb 16 '17 at 17:56
  • I was having two redirects - one to force SSL and then the next to apply the trailing slash. This brought those into one redirect and it made a difference in the load time of my site. Thank you! – Nathan Feb 7 '18 at 5:13
5

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 ^/?$ https://www.domain.com [R=301,L]

RewriteCond %{HTTP_HOST} ^domain\.com$ [OR]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule ^(.+?)/?$ https://www.domain.com/$1/ [R=301,L]

So my .htaccess file now looks like this:

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTPS} off
RewriteRule ^/?$ https://www.example.com/ [R=301,L]

RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule ^(.+?)/?$ https://www.example.com/$1/ [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>
  • 1
    "RewriteRule ^/?$ https://www.example.com[R=301,L]" - Probably just a typo, but you'll need a space before the flags. I would also include a slash on the end of the substitution, although the browser will fix this. – MrWhite Feb 16 '17 at 18:09
  • @w3dk include a slash on the end of the substitution Sorry, what do you mean? where? – Simon Hayter Feb 17 '17 at 15:26
  • So that line becomes RewriteRule ^/?$ https://www.example.com/ [R=301,L] (a slash after the hostname). It will work either way, so it doesn't strictly matter, but that's only because the browser fixes the URL by appending a slash if it is omitted. – MrWhite Feb 17 '17 at 16:12
  • @w3dk awesome! and [R=301,L] should be come [R=301, L] ? – Simon Hayter Feb 17 '17 at 16:53
  • No, just a space before the flags, ie. <space>[R=301,L]. No space inbetween the flags - you'll get a 500 error if you stick a space in there. – MrWhite Feb 17 '17 at 17:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.