强制使用.htaccess的子域使用SSL HTTPS [英] Force SSL HTTPS for subdomain with .htaccess

查看:89
本文介绍了强制使用.htaccess的子域使用SSL HTTPS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下代码,通常可以在网站上强制使用SSL:

I have this code for forcing SSL on a website generally:

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

我从此处获得

我希望它仅适用于特定的子域.

例如,如果您要访问http://sub.domain.org,则应将其重定向到https://sub.domain.org.

For example, if you were to visit http://sub.domain.org it should redirect to https://sub.domain.org.

我有一个预感,我需要添加另一个RewriteCond,基本上说是被击中的域是 http://sub.domain.org ,但我对如何编写此代码一无所知.

I have a hunch that I need to add another RewriteCond which basically says is the domain that is being hit is http://sub.domain.org, but I am at a loss on how to write this code.

我一直在寻找其他stackoverflow问题,例如:

I've been looking at other stackoverflow questions such as:

将HTTPS和WWW强制用于域,只有HTTPS用于子域HTACESS

使用.htaccess和mod_rewrite强制使用SSL/https

和Apache文档: https://httpd.apache.org /docs/current/mod/mod_rewrite.html#rewritecond

and the Apache docs: https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritecond

我做了这些尝试:

RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^(reports\.)?uksacb\.org$ [NC]
RewriteRule ^.*$ https://%1%{REQUEST_URI} [R,L]

在此问题/答案中,答案的排序相反:

Sort of reversed the answer in this question/answer:

RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} =reports.uksacb.org
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

但是,我仍然无法正确地理解如何做.

However I'm still failing to make sense of how to do it correctly.

如果有人可以向我解释我做错了什么,我需要做些什么才能正确解决问题,以及如何调试他们的重写条件和规则以确保其正常工作,我将非常感激.有时候,我认为我的浏览器正在缓存我编写的规则,而我的不同尝试没有受到任何影响!

If anyone could explain to me what I'm doing wrong, what I need to do to get it right and how does one go about debugging their rewrite conditions and rules to make sure it works properly I'd be very grateful. Sometimes I think my browser is caching the rules I've written and my different attempts are not taking any affect!

我的整个.htaccess看起来像这样:

My whole .htaccess looks like this:

选项-MultiViews

Options -MultiViews

RewriteEngine On

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

# Force SSL
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

推荐答案

为避免使用域名或子域名,我曾经使用以下代码:

To avoid using the domain or subdomain names I used to use the following code:

  # Redirect HTTP to HTTPS
  RewriteCond %{HTTPS} off
  RewriteCond %{HTTP:X-Forwarded-Proto} !https
  RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

我更喜欢没有WWW.

%{HTTPS} 如果通过SSL提供请求,则设置为on.

%{HTTPS} Set to on if the request is served over SSL.

%{HTTP_HOST} ,介于协议(http://或https://)和请求(/example-file.txt)之间.

%{HTTP_HOST} Everything between the protocol (http:// or https://) and the request (/example-file.txt).

%{REQUEST_URI} 服务器地址后的所有内容,例如对 http://example.com/my/example-file.txt 将此值设置为my/example-file.txt.

%{REQUEST_URI} Everything after the server address — for example, a request to http://example.com/my/example-file.txt sets this value as my/example-file.txt.

%{HTTP:X-Forwarded-Proto} :请求URL时使用的协议-设置为http或https.

%{HTTP:X-Forwarded-Proto} The protocol used when the URL was requested — set to http or https.

我希望它对任何人都有用.

I hope it can be useful for anybody.

这篇关于强制使用.htaccess的子域使用SSL HTTPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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