HTTP到HTTPS通过的.htaccessHTTP、HTTPS、htaccess

2023-09-02 09:36:11 作者:清风月影

我已经通过现有的问题看,但我还没有真正遇到任何事情对我的作品。

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.

我发现,code这一个片段中的.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.

任何帮助和建议,为AP preciated。

Any help and suggestions is appreciated.

干杯。

推荐答案

请尝试以下操作:

 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.

 
精彩推荐