htaccess的重定向HTTP到HTTPS [英] .htaccess redirect http to https

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

问题描述

我有一个旧的URL(www1.test.net),我想将其重定向到 https://www1.test.net
我已经实现,并在我的网站安装了我们的SSL证书。
这是我的旧文件的.htaccess:

I have an old url (www1.test.net) and I would like to redirect it to https://www1.test.net
I have implemented and installed our SSL certificate on my site.
This is my old file .htaccess:

RewriteEngine On
RewriteRule !\.(js|gif|jpg|png|css|txt)$ public/index.php [L]
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ public/$1 [L]

如何配置我的.htaccess文件,使网址自动重定向到https?
谢谢!

How can I configure my .htaccess file so that url auto redirect to https?
Thanks!

推荐答案

因为您的SSL端口未设置为80,这将工作:

given that your ssl-port is not set to 80, this will work:

RewriteEngine on

# force ssl
RewriteCond     %{SERVER_PORT} ^80$
RewriteRule     ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]

请注意,这应该是你的第一个重写规则。

Note that this should be your first rewrite rule.

编辑:这code确实下面。该的RewriteCond(银行足球比赛)检查阉请求在ServerPort为80(这是默认的HTTP端口,如果你指定的另一个端口,你将不得不调整状态的话)。如果是这样,我们匹配整个URL(。*),重定向到一个HTTPS的URL。 %{SERVER_NAME}可以与特定的URL来代替,但这样一来,你就不必改变code用于其他项目。 %{REQUEST_URI}是URL的TLD(顶级域)之后的部分,所以你会被重定向到你来自哪里,但作为HTTPS。

This code does the following. The RewriteCond(ition) checks wether the ServerPort of the request is 80 (which is the default http-port, if you specified another port, you would have to adjust the condition to it). If so, we match the whole url (.*) and redirect it to a https-url. %{SERVER_NAME} may be replaced with a specific url, but this way you don't have to alter the code for other projects. %{REQUEST_URI} is the portion of the url after the TLD (top-level-domain), so you will be redirected to where you came from, but as https.

这篇关于htaccess的重定向HTTP到HTTPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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