简体   繁体   中英

htaccess isn't redirecting to https

I'm trying to create an htaccess file in the root directory that will also affect subdomain directories (the subdomain works from a directory in the root).

Firstly, will this work if I have one htaccess file in the root? (will it work for subdomains in browsers (as they are actually just directories from root?) ).

The problem that I'm having is that when I try to visit http://demo.example.com , instead of it redirecting to https://demo.example.com it just remains using http://. This also is the case when trying to view http://www.example.com (it doesn't redirect to https).

This is my entire htaccess file as it currently stands. It seems quite bloated to me. Is there a way I can just redirect everything to https, regardless of subdomain / root ?

<IfModule mod_rewrite.c>
RewriteEngine On

# redirect subdomain to https
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.example\.com$ [NC]
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.com)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
</IfModule>

Can you try these rules in your site root .htaccess:

RewriteEngine On

# add www and https to main domain
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.com)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

# redirect everything to https
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

Test it in a new browser or completely clear your browser cache before testing.

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