简体   繁体   中英

Forcing non-www HTTPS from www.domain.com

I've looked at several threads here, but it seems like no rules function if www is present. I've also tried to remove the www, and afterwards force HTTPS. Could be some minor typo that I don't understand htaccess well enough to find. I have contacted the hosting service in case there's a problem with the cert/web server.

What I want is for HTTPS to be forced also when a visitor writes www.domain.com in the addressbar.

RewriteEngine on

#Enable HSTS
Header set Strict-Transport-Security "max-age=31536000" env=HTTPS

#Go to non-www if www
#RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

#Go to HTTPS it not already on
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

#check that filename is not a directory
RewriteCond %{REQUEST_FILENAME} !-d
#check that its a regular file
RewriteCond %{REQUEST_FILENAME}\.php -f
#remove what comes after the dot from url
RewriteRule ^(.*)$ $1.php

#check that filename is not a directory
RewriteCond %{REQUEST_FILENAME} !-d
#check that its a regular file
RewriteCond %{REQUEST_FILENAME}\.html -f
#remove what comes after the dot from url
RewriteRule ^(.*)$ $1.html

The issue was that there was a .htaccess file both in root, as well as in public_html. I was using the one in root, unaware that there was a .htaccess in public_html.

Now the one in root is removed, and it all works perfectly! Thanks for all replies.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM