1

I would like that all these addresses:

  1. http://www.example.com (currently correctly redirecting with my .htaccess file)
  2. http://example.com (currently correctly redirecting with my .htaccess file)
  3. https://www.example.com (this address does not redirect)

redirect to https://example.com.

With the following .htaccess file, I can only get the 1. and 2. addresses redirect to https://example.com.

Only https://www.example.com is still not redirecting to https://example.com.

Here is my .htaccess file

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# URL with www rewrite to https without www
RewriteCond %{HTTP_HOST} ^(www\.)(.*) [NC]
RewriteRule (.*) https://%2%{REQUEST_URI} [L,R=301]

# URL without www rewrite to https
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^(www\.)(.*) [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress
MrWhite
  • 43,179
  • 8
  • 60
  • 84
Reveclair
  • 2,399
  • 7
  • 37
  • 59
  • check [this answer](http://stackoverflow.com/a/9945803/5019802) it might be helpful. – Raunak Gupta Oct 05 '16 at 08:50
  • #3 Should be redirected by the first rule (in the same way it redirects #1). If it's not then _something else_ would seem to be at play here. However, you should avoid adding custom directives between the `# BEGIN WordPress` and `# END WordPress` comment markers since WordPress itself maintains this section and will try to overwrite these directives in future updates. – MrWhite May 31 '22 at 09:43

1 Answers1

2

Your question is surely a duplicate but I can't find a decent one to point to.

# URL with wrong domain to right one + https
RewriteCond %{HTTP_HOST} !=example.com
RewriteRule .* https://example.com/$0 [L,R=301]

# URL with no https fix (domain already correct otherwise first rule would have matched)
RewriteCond %{HTTPS} =off
RewriteRule .* https://%{HTTP_HOST}/$0 [L,R=301]
Walf
  • 8,535
  • 2
  • 44
  • 59