How to add HTTPS with WWW or non-WWW (.HTACCESS)

Do you want to add www or just use https://example.com/ without the www.

And also include https in your URL automatically.

Well, here’s the solution for you.

301 REDIRECT HTTP TO HTTPS AND NON-WWW TO WWW

# 301 REDIRECT HTTP TO HTTPS AND NON-WWW TO 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]
Header always set Content-Security-Policy "upgrade-insecure-requests;"
</IfModule>

And the other solution is here:

301 REDIRECT HTTP TO HTTPS AND WWW TO NON-WWW

# 301 REDIRECT HTTP TO HTTPS AND WWW TO 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]
Header always set Content-Security-Policy "upgrade-insecure-requests;"
</IfModule>

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.