4

I have a WordPress install where all the links are https://example.com. So, users that try to access the site via https://www.example.com will see the site, but none of the images and resources will load.

How do I force all users to be redirected to the same domain? I'd rather have everyone at https://example.com.

Is this something I can do through the domain's DNS settings, in the host/server, or in WordPress itself?

Update: I am using Apache.

Update 2:

All the conditions I am trying to catch:

User types,

  1. http://example.com
  2. http://www.example.com
  3. https://example.com
  4. https://www.example.com
  5. example.com
  6. www.example.com

As I understand, conditions 5 and 6 should be the same as 1 and 2. But I put it there, just in case. Condition 3 is where I want users to go, so that should work by default.

1-6 should redirect to -> https://example.com

1 Answer 1

4

What webserver are you running on?

Nginx

To remove www in nginx do the following.

if ($host = 'www.example.com' ) {
    rewrite  ^/(.*)$  http://example.com/$1  permanent;
}

That will strip the www.

To force https:

rewrite ^ https://$server_name$request_uri? permanent;

Along those lines.

Apache

Force https:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

Strip www:

RewriteCond %{HTTP_HOST} ^www\.(.+)$
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
5
  • Thanks I will give it a try and get back to you. I am on Apache. I should have noted that.
    – gdaniel
    Commented Jan 19, 2015 at 23:19
  • One warning- %{SERVER_NAME} may not always give you what you expect. Check it first. You can use PHP for this. You can always use the target domain name for this example if you need to.
    – closetnoc
    Commented Jan 20, 2015 at 1:54
  • Better to use a server block with Nginx like this: server { listen 443; server_name www.example.tld; return 301 https://example.tld$request_uri; ... } because if is evil and the official docs say "It's generally a good idea to avoid it if possible." Commented Jan 20, 2015 at 19:59
  • I think I got it to work. It's hard to say because different browsers are giving me different results. The redirect seems to work fine if the http is actually typed, but if the user only enter mydomain.com, then the user still get's the http page.
    – gdaniel
    Commented Jan 20, 2015 at 22:15
  • I updated my questions with all conditions that I am trying to catch.
    – gdaniel
    Commented Jan 20, 2015 at 22:36

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.