3

It's been asked a gazillion times but it's still not working in my Wordpress. I want to redirect everything to https://website.com. Current code been used:

<IfModule mod_rewrite.c>
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]
</IfModule>

based on this one but I also tried all in this page and of course this other one. Maybe I am missing something but this is what happens no matter what combination I tried:

# http://website.com      -> https://website.com
# https://website.com     -> https://website.com
# http://www.website.com  -> https://www.website.com     #wrong#
# https://www.website.com  -> https://www.website.com     #wrong#

I am starting to wonder whether the let's encrypt installation has something to do... or even some other modification done in wp-config.php... I added this in the past:

if ( defined( 'WP_CLI' ) ) {
    $_SERVER['HTTP_HOST'] = 'localhost';
}

define('WP_SITEURL', 'https://' . $_SERVER['HTTP_HOST'] . '/');
define('WP_HOME', 'https://' . $_SERVER['HTTP_HOST'] . '/');

Any idea? Thanks

4
  • Where exactly have you put the redirect directives in the .htaccess file in relation to the WordPress front-controller?
    – MrWhite
    Commented Dec 8, 2019 at 23:34
  • Hi, it's in the first lines of the htaccess, before anything else.
    – Adrihin
    Commented Dec 9, 2019 at 14:07
  • Are you using WP "pretty" permalinks? For for 4 examples... should that last one be from HTTPS, not HTTP as stated (otherwise it's just the same as the previous example)?
    – MrWhite
    Commented Dec 9, 2019 at 18:23
  • Apologies, I edited it now. My mistake. I answered you also below. And I am not using any plugin for standard permalinks (there is a section of the wp fully controlled by a plugin but it shouldn't affect as some pages are independent e.g. the main page, and redirects fail there too)
    – Adrihin
    Commented Dec 9, 2019 at 22:09

2 Answers 2

1

This is what you want:

RewriteEngine On
RewriteCond %{HTTP_HOST} website\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://website.com/$1 [R,L]

@elgoots, the reason yours was close but still had trouble is your HTTP_HOST has the www in it.

6
  • sigh. you are right. derp. this is why i shouldnt answer questions at 4am lol
    – Elgoots
    Commented Dec 11, 2019 at 2:17
  • 1
    You just made me a happy man. Thanks! I can't remember how many different combinations I tried! Thanks also to you Elgoots :)
    – Adrihin
    Commented Dec 11, 2019 at 15:09
  • @elgoots, yep me too, I try to only answer questions in the morning AFTER I've had a cup of coffee!
    – Trisha
    Commented Dec 12, 2019 at 15:48
  • But this is "only" an HTTP to HTTPS redirect. This will fail to canonicalise the www subdomain in a request for https://www.website.com (as stated in the question) - leaving it up to WordPress itself to canonicalise the request later (which it may well be doing). The first condition is superfluous - as it will always be successful (assuming we are dealing with just a single domain).
    – MrWhite
    Commented Dec 12, 2019 at 19:20
  • "your HTTP_HOST has the www in it" - I assume you are referring to the first code block in @Elgoots answer? Yes, true, but WordPress itself should have then canonicalised the www subdomain. And this does not apply to the 2nd code block, given the order of the two rules (since the www subdomain would already have been removed by the time the second rule was processed).
    – MrWhite
    Commented Dec 12, 2019 at 19:29
0

Setting the wordpress home and site url manually to https://website.com

Then remove all other rules in .htaccess and have:

RewriteEngine On 
RewriteCond %{HTTPS} off 
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

The htaccess will redirect to https for all traffic and all pages/posts/categories etc.

As for dropping the www infront, the site url and homepage url should take care of that in the wordpress framework.

Give it a try and report back.

If that does not work, try the following and manually set the request uri. Obviously change website.com with your domain:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.website.com$ [NC]
RewriteRule ^(.*)$ https://website.com/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
7
  • Hi, I removed the lines from the wp-config and this is what happened: # http://website.com -> https://website.com # https://website.com -> https://website.com # http://www.website.com -> https://website.com #this one changed, now OK# # https://www.website.com -> https://www.website.com #wrong# what is exactly request uri?
    – Adrihin
    Commented Dec 9, 2019 at 14:19
  • @AdrianInfante Was that without changing your existing .htaccess directives?
    – MrWhite
    Commented Dec 9, 2019 at 18:21
  • @MrWhite, following Elgoots comments, the first thing I did was to remove the code from the wp-config and do the home and site set manually. From that point on, the only redirection working wrong was www to https://. I changed afterwards htaccess as recommended but it didn't do any other change. At the moment I am with 3 redirections OK, 1 wrong.
    – Adrihin
    Commented Dec 9, 2019 at 22:06
  • So are you saying now using my suggestion: https://www.website.com redirects to https://www.website.com So the issue is that it did not remove the www. infront ?
    – Elgoots
    Commented Dec 10, 2019 at 1:12
  • @Elgoots exactly!
    – Adrihin
    Commented Dec 10, 2019 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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