.htaccess to force https and remove www

(Paulraphaelson) #1

Hi, I’m trying to come up with a simple rule to convert all requests to https://domain.com.
So whether there’s a www or not, or an http, I want it all to resolve there.

I found this suggestion, but it gave a page not found error:

RewriteEngine On
RewriteBase /

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


Thanks for any help.

(Paulraphaelson) #2

I see another thread on how to simultaneously redirect to https and www (I’m going for non-www).

Here’s an adaptation of that code:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^paulraphaelson.com [NC,OR]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://paulraphaelson.com/$1 [R=301,L]

Does this look sound?

(Owais alam) #3

Hi @paul.raphaelson

You have to try the following code

RewriteEngine On
RewriteCond %{HTTPS}        =off   [OR]
RewriteCond %{HTTP_HOST}    !^example\.com$
RewriteRule ^(.*)$          "https://example.com/$1" [R=301,L]

You can then change the WordPress address and site URL by visiting WordPress settings page.

(Paulraphaelson) #4

Tried it. Here’s the exact syntax:

RewriteEngine On
RewriteCond %{HTTPS} =off [OR]
RewriteCond %{HTTP_HOST} !^words.paulraphaelson.com$
RewriteRule ^(.*)$ “https://words.paulraphaelson.com/$1” [R=301,L]

This breaks the site. Internal server error.

(Razy) #5

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.(.)$ [NC]
RewriteRule ^(.
)$ http://%1/$1 [R=301,L]

Save and exit. The changes should go into effect immediately. Note that if you are using HTTPS, you should update “http”, in the RewriteRule line, to “https”.

(Paulraphaelson) #6

Thank you. That’s the code that I’m using now. It does indeed redirect http://www.domain.com, but it doesn’t redirect http://domain.com.

(Joshua Baer) #7

Have you set WP-Admin>Settings>General to https://words.paulraphaelson.com/ in both fields?

(Paulraphaelson) #8

It’s not a Wordpress site. Just simple static HTML.

(Arne Brockmann) #9

This worked for me:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.your_domain.com$
RewriteCond %{SERVER_PORT} ^443
RewriteRule ^(.*)$ https://your_domain.com/$1 [R=301]

From this source: https://www.lcwsoft.com/knowledgebase/66/How-do-I-redirect-www-to-non-www-or-vice-versa-http-and-https.html

(Henk) #10

After unsuccesfully trying these things and talking to support this is what worked for Cloudways:

RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^yourdomain.com$ [NC]
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R=301,L]