删除www站点范围,在某些目录上强制https,在其余目录上删除http?

Ant*_*ony 13 .htaccess mod-rewrite ssl https no-www

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

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

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

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

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

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

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

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* ./index.php
Run Code Online (Sandbox Code Playgroud)

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

提前致谢!

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

@Gumbo根据你的建议,这是我完整的.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
Run Code Online (Sandbox Code Playgroud)

导航到http:// example/login仍然会转到http://example/index.php,它应该转到https:// example/login我的订单是否有错?

Gum*_*mbo 27

试试这些规则:

# 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]
Run Code Online (Sandbox Code Playgroud)

您还可能希望添加一些其他条件,以仅更改GET和HEAD请求上的协议,但不更改POST请求.


Ale*_*vić 8

这段代码对我有用:

<IfModule mod_rewrite.c>

   RewriteEngine on

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

   RewriteCond %{HTTPS} !=on
   RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

</IfModule>
Run Code Online (Sandbox Code Playgroud)