简体   繁体   English

始终将http重定向到https

[英]Always redirect http to https

This is the code I have so far: 这是我到目前为止的代码:

<IfModule !mod_rewrite.c>
    LoadModule rewrite_module modules/mod_rewrite.so
</IfModule>
<IfModule mod_rewrite.c>
    RewriteEngine on
    ReWriteCond %{SERVER_PORT} !^443$
    RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]
</IfModule>

Ideally I am trying to achieve a situation where any request to: 理想情况下,我正在尝试实现以下要求:

http://domain.com/

and/or 和/或

http://www.domain.com/ 

is redirected to 重定向到

https://domain.com/whatever/sub/directory

So far the attempts I have made only redirect the root domain to the HTTPS, when I go into some sub directory of the site and remove the HTTPS from the URL, it doesn't redirect it back to the HTTPS, it just stays on HTTP. 到目前为止,我所做的尝试仅将根域重定向到HTTPS,当我进入站点的某个子目录并从URL中删除HTTPS时,它不会将其重定向回HTTPS,而是停留在HTTP上。 。

I am a major newbie at htaccess so any advice is appreciated. 我是htaccess的主要新手,因此欢迎您提出任何建议。

Remove leading slash: 删除前导斜杠:

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [NE,R=301,L]
  • .htaccess is per directory directive and Apache strips the current directory path (thus leading slash) from RewriteRule URI pattern. .htaccess是针对每个目录指令的,Apache从RewriteRule URI模式中剥离当前目录路径(因此使用斜杠)。

If the ssl.conf file does not exist, you may have to install the mod_ssl module If the file - /etc/httpd/conf.d/ssl.conf exists, comment out everything in it from the header to the header including the headers themselves. 如果ssl.conf文件不存在,则可能必须安装mod_ssl模块。如果文件-/etc/httpd/conf.d/ssl.conf存在,请注释掉其中的所有内容,从标头到标头(包括标头)他们自己。

Make sure the "LoadModule ssl_module modules/mod_ssl.so" directive is present and not commented out in the file. 确保存在“ LoadModule ssl_module modules / mod_ssl.so”指令,并且在文件中未将其注释掉。 Otherwise add it. 否则添加它。

Modify the /etc/httpd/conf/httpd.conf file. 修改/etc/httpd/conf/httpd.conf文件。 Add the following part to the end - 将以下部分添加到末尾-

NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.example.com
DocumentRoot /var/www/html
Redirect permanent / https://www.example.com/
</VirtualHost>
<VirtualHost *:443>
    ServerName www.example.com
    DocumentRoot /var/www/html
    SSLEngine on
    SSLCertificateFile /var/cert/abcd344.crt
    SSLCertificateKeyFile /var/cert/www_example_com.key
    SSLCertificateChainFile /var/cert/gd_bundle-g2-g1.crt
</VirtualHost>

The above settings will redirect all requests to port 443 (HTTPS). 上述设置会将所有请求重定向到端口443(HTTPS)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM