apache 将 http 重定向到 https,将 www 重定向到非 www [英] apache redirect http to https and www to non www
问题描述
基本上我想要的是重定向请求以使用 HTTPS 而不是 http
basically what i want is redirect al request to use HTTPS instead of http
到目前为止,我的 htaccess 中有这个,而且效果很好:代码:
I have this in my htaccess so far and it worked great: Code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</ifModule>
今天有人注意到,当去:http://www.example.com 它重定向到并显示不安全的连接内容.
today someone noticed that when going to: http://www.example.com it redirects to and shows an unsecure connection thingie.
我的 ssl 是为非 www 域设置的:mydomain.com
My ssl is setup for non www domain: mydomain.com
所以我需要确保所有站点请求都发送到非 www 和 https:如果我把 example.com 它重定向到 https://example.com
So i need to make sure all site requests are sent to non www and https: It works fine if i put example.com it redirects to https://example.com
但是对于 www.example.com,它转到 htts://www.example.com 并显示错误
but with www.example.com it goes to htts://www.example.com and shows the error
我需要在代码中添加什么才能将 www 重定向到非 www 然后重定向到 ssl?
what do i need to add to my code to redirect www to non www and then to ssl ?
推荐答案
你可以从 HTTP_HOST 得到你需要的
You can get what you need from the HTTP_HOST
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www.)?(.*)$ [NC]
RewriteRule (.*) https://%1%{REQUEST_URI} [L,R=301]
这样它就会让主机始终没有子域.
This way it will get the host always without the subdomain.
这篇关于apache 将 http 重定向到 https,将 www 重定向到非 www的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!