We are using cPanel for some websites and we use the following .htaccess rules and for the most part they work perfectly but they have a few issues...
# Force HTTPS & NON-WWW
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
</IfModule>
With the rule below, if we visit non-www links they correctly get switched to www. links which is good
# Force HTTPS & WWW
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
</IfModule>
The problem with this Force HTTPS & WWW
rule is when we come to subdomains...
staging.website.com
it incorrectly gets rewritten to www.staging.website.com
. Sure a condition like RewriteCond %{HTTP_HOST} !^staging\\. [NC]
RewriteCond %{HTTP_HOST} !^staging\\. [NC]
could be used but that means every subdomain would need one setup. https://
and one is https://www.
and for that particular cPanel account I don't know how I can modify these .htaccess rules to work for both scenarios... #1 and #2 are most noticeable when it comes to ipv6.website.com
https://website.com
--> https://www.website.com
http://website.com
--> https://www.website.com
https://www.website.com
--> https://www.website.com
http://www.website.com
--> https://www.website.com
https://ipv6.website.com
--> https://ipv6.website.com
http://ipv6.website.com
--> https://ipv6.website.com
https://www.ipv6.website.com
--> https://ipv6.website.com
http://www.ipv6.website.com
--> https://ipv6.website.com
https://randomsubdomain.website.com
--> https://www.randomsubdomain.website.com
http://randomsubdomain.website.com
--> https://www.randomsubdomain.website.com
https://www.randomsubdomain.website.com
--> https://www.randomsubdomain.website.com
http://www.randomsubdomain.website.com
--> https://www.randomsubdomain.website.com
https://ftp.website.com
--> http://ftp.website.com
http://ftp.website.com
--> http://ftp.website.com
https://www.ftp.website.com
--> http://ftp.website.com
http://www.ftp.website.com
--> http://ftp.website.com
https://website.com
--> https://website.com
http://website.com
--> https://website.com
https://www.website.com
--> https://website.com
http://www.website.com
--> https://website.com
https://ipv6.website.com
--> https://ipv6.website.com
http://ipv6.website.com
--> https://ipv6.website.com
https://www.ipv6.website.com
--> https://ipv6.website.com
http://www.ipv6.website.com
--> https://ipv6.website.com
https://randomsubdomain.website.com
--> https://randomsubdomain.website.com
http://randomsubdomain.website.com
--> https://randomsubdomain.website.com
https://randomsubdomain.website.com
--> https://www.randomsubdomain.website.com
http://randomsubdomain.website.com
--> https://www.randomsubdomain.website.com
https://ftp.website.com
--> http://ftp.website.com
http://ftp.website.com
--> http://ftp.website.com
https://www.ftp.website.com
--> http://ftp.website.com
http://www.ftp.website.com
--> http://ftp.website.com
It appears you need to have 2 separate redirect rules here:
www
and https://
https://
onlyYou can have your .htaccess as this:
<IfModule mod_rewrite.c>
RewriteEngine On
# enforce http:// and non-www for ftp domain:
RewriteCond %{HTTPS} on [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(ftp\..+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,NE,R=301]
RewriteCond %{HTTP_HOST} ^ftp\. [NC]
RewriteRule ^ - [L]
# Force HTTPS & WWW for any subdomain not in list
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !(?:^|\.)(?:ipv6|mail|mobile|staging|cpanel)\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
# Force HTTPS & non-WWW for listed sub domains
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?((?:ipv6|maill|mobile|staging|cpanel)\..+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
</IfModule>
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.