我有一个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叙述上
#跟踪符号链接。
选项+的FollowSymLinks
的RewriteCond%{HTTP_HOST}!^ $
的RewriteCond%{HTTP_HOST}!^ WWW 。 [NC]
的RewriteCond%{} HTTPS关闭
重写规则^ https://开头%{HTTP_HOST}%{REQUEST_URI} [R = 301,L,NE]
#prevent目录列表
选项所有-Indexes
解决方案
替换您当前的code。通过这一个
选项-Indexes +的FollowSymLinks
RewriteEngine叙述上
#重定向www的域名https://example.com
的RewriteCond%{HTTP_HOST} ^ WWW 。(。+)$ [NC]
重写规则^ $ HTTPS(*)://%1 / $ 1 [R = 301,L]
#重定向HTTP到HTTPS(在这一点上,域名是没有WWW)
的RewriteCond%{HTTPS} =关闭
(。*)重写规则^ $的https://%{HTTP_HOST} / $ 1 [R = 301,L]
I have a problem with .htaccess
I have a certificate in my server, and it's only for https://example.com (not https://www.example.com) Therefore, I'm trying to do a .htaccess redirect to the correct url.
My intention is this:
http://www.example.com --> https://example.com
http://example.com --> https://example.com
https://www.example.com --> https://example.com
I tried different combinations and nothing seems to work. At the moment I have this, but seems like is not working for
http://www.example.com/subfolder
, I don't know what is failing...
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
解决方案
Replace your current code by this one
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]