.htaccess非www到www以及http到https [英] .htaccess non-www to www and http to https

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

问题描述

我知道这个问题已经被问过很多次了,但是我花了最后3个小时来回答人们的问题,但似乎没有一个适合我.

I know this question has been asked and answered many times but I've spent the last 3 hours going through peoples answers and none have seemed to work just right for me.

我需要一些.htaccess代码来做到这一点:

I need some .htaccess code to do this:

如果域是example.co.uk/$urlData(仅适用于主域,没有子域),也要重写www.example.co.uk/$urlData

If domain is example.co.uk/$urlData (only apply to main domain and no subdomains) rewrite too www.example.co.uk/$urlData

如果HTTPS不在"on"上,并且域是主域(即没有子域),则重定向到https://www.example.co.uk/$urlData.

If HTTPS is not "on" and domain is primary (ie. no subdomains) then redirect to https://www.example.co.uk/$urlData.

我必须使用RewriteCond %{HTTP:X-Forwarded-Proto} !https来测试HTTPS是否已关闭,因为服务器上未配置RewriteCond %{HTTPS} off.

I have to use RewriteCond %{HTTP:X-Forwarded-Proto} !https to test if HTTPS is off as RewriteCond %{HTTPS} off is not configured on my server.

推荐答案

将此添加到您的.htaccess:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^example\.co\.uk$ [NC]
RewriteRule (.*) https://www.example.co.uk/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^www\.example\.co\uk$ [NC]
RewriteCond %{HTTP:X-Forwarded-Proto} !=https
RewriteRule (.*) https://www.example.co.uk/$1 [R=301,L]

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

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