http 到 https 通过 .htaccess [英] http to https through .htaccess

查看:50
本文介绍了http 到 https 通过 .htaccess的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经浏览了现有的问题,但我还没有真正遇到任何对我有用的问题.

I've had a look through existing questions, but I haven't really come across anything which works for me.

我目前正在运行一个具有安全 SSL 证书的站点.它可以在 https://www.example.co.uk 访问,但问题是该站点也可以在 http://www.example.co.uk 访问 - 我不希望这是可能的.我需要它从 http 重定向到 https.

I'm currently running a site with a Secure SSL certificate. It can be accessed at https://www.example.co.uk a problem is the site can also be accessed at http://www.example.co.uk - I don't want this to be possible. I need it to redirect from http to https.

我在 .htaccess 文件中找到了这段代码.

I found this one snippet of code to use in an .htaccess file.

Options +FollowSymLinks 
RewriteEngine on 
RewriteCond %{HTTP_HOST} ^example.co.uk [NC] 
RewriteRule ^(.*)$ https://example.co.uk/$1 [L,R=301]

当用户在地址栏中输入 example.co.uk 时,这工作正常,但我还需要添加某种条件语句,以便如果用户输入www.example.co.uk"或http://www.example.co.uk'.

This works fine when the user enters example.co.uk into their address bar, but I also need to add a conditional statement of some sort so that if the user enters 'www.example.co.uk' or 'http://www.example.co.uk'.

我尝试过使用 [OR] 之类的方法,但这最终会导致服务器错误.

I've tried using the likes of [OR], but this ends up creating server errors.

感谢任何帮助和建议.

干杯.

推荐答案

尝试以下操作:

 RewriteEngine On
 RewriteCond %{HTTPS} !=on
 RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

此外,您还可以根据端口号进行重定向,例如:

Also, you can also redirect based on port number, for example:

 RewriteCond %{SERVER_PORT} ^80$
 RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

这会将在端口 80 上收到的所有请求重定向到 HTTPS.

This will redirect all requests received on port 80 to HTTPS.

这篇关于http 到 https 通过 .htaccess的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆