通过 httpd.conf 将 http 重定向到 https
Posted
技术标签:
【中文标题】通过 httpd.conf 将 http 重定向到 https【英文标题】:redirect http to https through httpd.conf 【发布时间】:2017-12-24 22:02:16 【问题描述】:需要把http改成https。
http://myServerName/itf/sheldon/imex/ajax/share_details/v1/CH0012221716/issuer/contact.json **Should change to** https://myServerName//itf/sheldon/imex/ajax/share_details/v1/CH0012221716/issuer/contact.json
为此,我在 httpd.conf 文件中包含以下内容
*LoadModule rewrite_module modules/mod_rewrite.so
RewriteCond %HTTPS off
RewriteEngine On
RewriteRule ^(.*)(\/itf)(\/sheldon\/)(.*)\.(json|JSON)$ https://myServerName/$2$3$4.json [L,R=301,NC]
RewriteRule "^(.*)(\/itf)(\/fqs\/)(.*)\.(json|JSON)(.*)$" https://myServerName/itf/fqs/$4.json$6 [R=301,L,NC]
Include conf/vhost.conf*
The vhost.conf file have necessary configuration for virtualhost:443
So I am able to get the result if I use https://myServerName/....,
**but not able to get the result with** http://myServerName/...
【问题讨论】:
【参考方案1】:设置您的虚拟主机文件/httpd.conf 以包含行
<VirtualHost *:80>
ServerName enter.yoursite.com
Redirect / https://enter.yoursite.com/
</VirtualHost>
<VirtualHost *:443>
ServerName enter.yoursite.com
ServerAdmin some@email.com
Insert EVERYTHING else in here (rewrite rules, log file stuff, etc)
</VirtualHost>
这将强制网站始终重定向到 HTTPS,然后在 HTTPS 下加载您想要的所有配置。
【讨论】:
【参考方案2】:我找到了解决方案: 要将特定或任何 http 调用更改为 https 调用,请将代码放入
VirtualHost :80
ErrorLog /var/log/httpd/error_log
CustomLog logs/access.log common
LogLevel info
RewriteCond %HTTPS off
RewriteEngine On
RewriteRule ^(.*)$ https://%HTTP_HOST$1 [R=301,L]
RewriteEngine On
【讨论】:
以上是关于通过 httpd.conf 将 http 重定向到 https的主要内容,如果未能解决你的问题,请参考以下文章
重写 WordPress 主题文件,将所有内容重定向到 httpd.conf 中的 index.php
通过 httpd.conf / htaccess 重定向目录中除一个文件之外的所有文件
用于将 ip 重定向到主机名的 Apache httpd.conf
将所有 URL 重定向到 httpd.conf 中的单个页面