By Niklas


2019-01-06 11:51:13 8 Comments

After implementing ssl on wordpress, I wanted to force a redirection to a site with the following characteristics:

non www, ssl, with trailing slash

Unfortunately it only works in some cases.

For instance, instead of redirecting from a site with these characteristics (http, non www, without trailing slash to https, non www with trailing slash), it redirects like this:

  • http://example.com/foo over https://example.com/foo to https://example.com/foo/.

I also have an issue with redirecting a www, http site without trailing slash to an https, non www, with trailing slash site.

Currently, it redirects from http://www.example.com/foo via http://example.com/foo to https://example.com/foo and https://example.com/foo/.

Therefore my question is: How can I fix this? Is this done somehow by the Wordpress system?

Enclosed you'll find my .htaccess file. I hope you can help me.

RewriteEngine On
RewriteBase /

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

RewriteCond %{REQUEST_URI} !wp-content\/cache\/(all|wpfc-mobile-cache)

Could this be somehow caused by WordPress? I've mentioned this before, but there is also a rewrite code from WordPress.

# 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>

1 comments

@MrWhite 2019-01-06 19:21:31

If you are doing this in .htaccess then I wouldn't try to do this in a single redirect. The longest "chain" should be two redirects (1, 2 or even 3 redirects makes no difference for SEO):

  1. Canonicalise the HTTP to HTTPS and www to non-www in the first redirect
  2. Append the trailing slash in the second redirect.

(If, however, you are implementing HSTS then you would need to implement the HTTP to HTTPS redirect (on the same hostname) first. Then canonicalise the subdomain. This potentially makes a maximum of 3 redirects.)

This appears to be what you are seeing in your first example.

It's possible to append the trailing slash in .htaccess, however, you've not shown this code so I'm assuming WordPress is configured to do this?

Currently, it redirects from http://www.example.com/foo via http://example.com/foo to https://example.com/foo and https://example.com/foo/.

However, this doesn't correlate with the directives you posted, assuming these are at the top of your .htaccess file:

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

These directives only redirect to https://, so it's not clear where your second stage redirect to http://example.com/foo is coming from? Unless maybe you are seeing a cached response?

Presumably, you are already linking to URLs with a trailing slash throughout your application, and you previously implemented a canonical www to non-www redirect, so any requests for http://www.example.com/foo should be a relatively rare occurrence. (?)

Related Questions

Sponsored Content

1 Answered Questions

How do I secure a subdomain using UCC SSL?

2 Answered Questions

site on subdomain is redirecting to main site after installing wildcard ssl cert on both

  • 2017-07-27 18:23:20
  • Dan1ell
  • 515 View
  • 0 Score
  • 2 Answer
  • Tags:   ssl

1 Answered Questions

SSL certificate breaks CSS (in combination with W3TC)

  • 2016-12-31 11:20:30
  • Steve Mueller
  • 953 View
  • 0 Score
  • 1 Answer
  • Tags:   ssl

0 Answered Questions

2 Answered Questions

[SOLVED] How to fix too many redirects when enabling SSL for site?

  • 2018-05-27 12:46:07
  • Joel G Mathew
  • 3754 View
  • 0 Score
  • 2 Answer
  • Tags:   redirect ssl

1 Answered Questions

[SOLVED] 'Too many redirects' error after changing site URL in Wordpress

4 Answered Questions

[SOLVED] https multiple redirects

1 Answered Questions

[SOLVED] Adding a SSL Certificate

2 Answered Questions

[SOLVED] 301 Redirect Loop

  • 2015-03-01 14:19:23
  • Harikesh
  • 1284 View
  • 0 Score
  • 2 Answer
  • Tags:   redirect htaccess

3 Answered Questions

Sponsored Content