在整个站点范围内删除 www,在某些目录上强制使用 https,在其余目录上强制使用 http? [英] Remove www site-wide, force https on certain directories and http on the rest?

查看:29
本文介绍了在整个站点范围内删除 www,在某些目录上强制使用 https,在其余目录上强制使用 http?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我想删除 www.来自我的域名

Firstly, I would like to remove the www. from my domain name

http://www.example.com => http://example.com

我还希望某些目录是安全的 (https),​​而其余目录仍然是 http

I would also like for certain directories to be secure (https), while the rest remain http

http://example.com/login => https://example.com/login

显然它需要适用于所有条件,例如如果有人输入:

Obviously it needs to work for all conditions, for example if someone types in:

http://www.example.com/login => https://example.com/login

此外,当人们离开登录页面时,它会返回到 http.目前在 .htaccess 中的是以下由我正在使用的软件自动完成的,我想需要在那里才能工作:

Also when people navigate away from the login page it returns to http. Currently in the .htaccess is the following which is done automatically by the software I am using and I suppose needs to be there for it to work:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* ./index.php

关于如何实现梦想控制所有这些事情的任何想法?

Any ideas on how I can achieve dream control all these things?

提前致谢!

====================

===================

@Gumbo 按照您的建议,这是我完整的 .htaccess

@Gumbo Following your advice this is my complete .htaccess

RewriteEngine On


# remove www from host
RewriteCond %{HTTP_HOST} ^www.(.+)
RewriteCond %{HTTPS}s/%1 ^(on(s)|offs)/(.+)
RewriteRule ^ http%2://%3%{REQUEST_URI} [L,R=301]

# force HTTPS
RewriteCond %{HTTPS} =off
RewriteRule ^(login)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# force HTTP
RewriteCond %{HTTPS} =on
RewriteRule !^(login)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]


RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* ./index.php


Options -Indexes

导航到 http://example/login 仍然转到 http://example/index.php 什么时候应该转到 https://示例/登录 我的顺序有误吗?

Navigating to http://example/login still goes to http://example/index.php when it should go to https://example/login Do I have the order wrong?

推荐答案

试试这些规则:

# remove www from host
RewriteCond %{HTTP_HOST} ^www.(.+)
RewriteCond %{HTTPS}s/%1 ^(on(s)|offs)/(.+)
RewriteRule ^ http%2://%3%{REQUEST_URI} [L,R=301]

# force HTTPS
RewriteCond %{HTTPS} =off
RewriteRule ^(login|foo|bar|…)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# force HTTP
RewriteCond %{HTTPS} =on
RewriteRule !^(login|foo|bar|…)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

您可能还想添加一些额外的条件,以仅更改 GET 和 HEAD 请求的协议,而不更改 POST 请求的协议.

You also might want to add some additional conditions to only change the protocol on GET and HEAD requests but not on POST requests.

这篇关于在整个站点范围内删除 www,在某些目录上强制使用 https,在其余目录上强制使用 http?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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