简体   繁体   中英

How to redirect all website requests to HTTPS with no www

I would like to make it so that all requests to my website are redirected to HTTPS with no www. Right now I apparently have both a www and non www version of my site which is supposed to be bad for seo (duplicate content). I also just added an SSL certificate and need to redirect all traffic to the https version of my site. Below is an example of what I am looking to accomplish:

example.com -> https://example.com
www.example.com -> https://example.com
http://www.example.com -> https://example.com
https://www.example.com -> https://example.com

How would I accomplish this within my htaccess file?

try this:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
</IfModule>

These rules need to be at the top of the htaccess file in your document root

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

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