apache - htaccess : Redirect to https not working with out www

标签 apache .htaccess ssl redirect

我刚刚在 CentOS 7.3 的帮助下安装了 Origin CA 证书 link 然后,我使用以下 将所有 http 请求重定向到 https 并将 www 重定向到 non-www。 htaccess 命令

RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]

这里有以下问题,

1) 当我设置 RewriteCond %{HTTPS} off 时,它给出了 Too Many Redirects 错误

2) 当我访问带有以下 URL 的站点时 http://website.com , 它不会重定向到 https

3) 但是,http://www.website.com工作正常(与 WWW)

不确定我是否在 SSL 配置中做错了什么,我们将不胜感激。

最佳答案

您可以用这条规则替换 http->httpswww->non-www 重定向规则:

RewriteEngine On

# remove www and turn on https rule
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTP:X-Forwarded-SSL} =off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]

关于apache - htaccess : Redirect to https not working with out www,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44808139/

相关文章:

java - OpenLDAP 和 SSL

php - Apache RewriteRule 规则

php - 超简单的 HTTP 套接字服务器,用 PHP 编写,表现出乎意料

php - 同一应用程序是否可以有多个 .htaccess 文件?

php - 带有可选字段的 REST API 端点 .htaccess (PHP)

security - 去除 HTTPS 页面中的 IE 警告

linux - 允许传入流量到 CentOS 服务器

Python Django httpd - [通知] child pid XXXXX exit signal Segmentation fault (11)

php - .htaccess 阻止子目录访问

java - Sonar java漏洞方法返回true