使用.htaccess重定向到https

我有.htaccess的问题

我的服务器上有一个证书,它只用于https://example.com(而不是https://www.example.com)因此,我正在尝试将.htaccess重定向到正确的URL.

我的意图是:

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

我尝试了不同的组合,似乎没有任何效果.目前我有这个,但似乎不适合

http://www.example.com/subfolder,我不知道什么是失败的……

RewriteEngine On

# Follow symbolic links.
Options +FollowSymLinks

RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

# Prevent directory listings
Options All -Indexes

最佳答案 用这个替换你当前的代码

Options -Indexes +FollowSymLinks
RewriteEngine On

# redirect "www" domain to https://example.com
RewriteCond %{HTTP_HOST} ^www\.(.+)$[NC]
RewriteRule ^(.*)$https://%1/$1 [R=301,L]

# redirect http to https (at this point, domain is without "www")
RewriteCond %{HTTPS} =off
RewriteRule ^(.*)$https://%{HTTP_HOST}/$1 [R=301,L]
点赞