繁体   English   中英

将所有流量重定向到HTTPS(一个目录除外)应强制使用HTTP

[英]Redirect all traffic to HTTPS except for one directory should force HTTP

我尝试从许多帖子中重写条件,但似乎没有任何效果。

我想在整个站点上强制使用HTTPS 除了一个目录必须强制使用HTTP

www.example.com都应为HTTPS

dir3任何内容: www.example.com/dir1/dir2/dir3/test.php都应强制为HTTP

这是我到目前为止所拥有的。

RewriteEngine On

# Redirect HTTP traffic to HTTPS
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

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

我想出了一些可行的方法,但这可能不是最优雅的解决方案。

RewriteEngine On

# Redirect HTTP traffic to HTTPS, except dir3 directory
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/dir1/dir2/dir3/
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

使用Redirect和RedirectMatch指令可以精确控制哪些区域被强制设置为http,哪些区域被强制设置为https。 如果要客户端浏览器缓存重定向(即,如果它确实是永久的),则将永久关键字用于301重定向,或者对于302(临时)重定向,则将其忽略。 请注意,在测试是否使用永久性时,浏览器将缓存重定向,并且在此之后所做的任何更改都将不会显示...在浏览器中清除重定向缓存可能会令人沮丧。

<VirtualHost *:80>
    ServerName www.example.com

    # Redirect everything except /dir1/dir2/dir3 to use https
    RedirectMatch permanent "^(/(?!dir1/dir2/dir3/?).*)" https://www.example.com$1

    # config for the http://www.example.com/dir1/dir2/dir3/ stuff goes here
</VirtualHost>

<VirtualHost *:443>
    ServerName www.example.com

    # Redirect /dir1/dir2/dir3 to use http
    Redirect permanent /dir1/dir2/dir3 http://www.example.com/dir1/dir2/dir3

    # config for the https://www.example.com/ stuff goes here
</VirtualHost>

为了使事情保持井井有条,您可能还希望同时在端口80和端口443上重定向example.com,以确保始终通过www.example.com访问您的站点。 您可以改用上面的VirtualHosts中的ServerAlias,但是指定重定向将强制客户端浏览器中的名称始终显示www.example.com,而不显示请求来自的名称。 大多数网站的首选是强迫所有人(重定向)到www.example.com。

<VirtualHost *:80>
    ServerName example.com
    Redirect permanent /dir1/dir2/dir3 http://www.example.com/dir1/dir2/dir3
    Redirect permanent / https://www.example.com/
</VirtualHost>

<VirtualHost *:443>
    ServerName example.com
    Redirect permanent /dir1/dir2/dir3 http://www.example.com/dir1/dir2/dir3
    Redirect permanent / https://www.example.com/
</VirtualHost>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM