【问题标题】:How to force HTTPS on all URL's except one directory /images using .htaccess?如何使用 .htaccess 在除一个目录 /images 之外的所有 URL 上强制使用 HTTPS?
【发布时间】:2022-01-07 03:17:00
【问题描述】:

我正在使用 WordPress,我们有一个不是 WordPress 目录 /images 的目录,我们需要此目录为 HTTP,只有其他所有内容都应强制为 HTTPS。

在 WordPress 设置中,我们将域设置为 HTTP

.htaccess 文件中,我们有以下内容。

我似乎无法让它工作。我们的主机是 cloudways,如果这有帮助的话

# This file was updated by Duplicator on 2018-09-10 16:52:27. See .htaccess.orig for the original .htaccess file.
# Please note that other plugins and resources write to this file. If the time-stamp above is different
# than the current time-stamp on the file system then another resource has updated this file.
# Duplicator only writes to this file once during the install process while running the installer.php file.

#RewriteEngine On

#RewriteCond %{HTTP:X-Forwarded-SSL} !on
#RewriteCond %{REQUEST_URI} ^\/(images)
#RewriteRule (.*) https://%{HTTP_HOST}/$1 [L,R=301]

#RewriteCond %{HTTP:X-Forwarded-SSL} =on
#RewriteCond %{REQUEST_URI} !^\/(images)
#RewriteRule (.*) http://%{HTTP_HOST}/$1 [L,R=301]

RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI}

# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress



# MalCare WAF
<Files ".user.ini">
<IfModule mod_authz_core.c>
  Require all denied
</IfModule>
<IfModule !mod_authz_core.c>
  Order deny,allow
  Deny from all
</IfModule>
</Files>

# END MalCare WAF

【问题讨论】:

  • "not a wordpress directory /images" - 当您说它“不是 WordPress 目录”时,我认为您的 WordPress 网站没有以任何方式使用它? IE。您的页面都没有引用/images 子目录中的图像?不确定为什么您似乎试图在您发布的两条规则中从 HTTPS 重定向回 HTTP?我以为你想强制 HTTPS,除了特定的子目录?

标签: wordpress .htaccess mod-rewrite https url-rewriting


【解决方案1】:

在 wordpress 设置中,我们将域设置为 http

如果您想在除“WordPress 之外”的一个目录之外的任何地方强制使用 HTTPS,那么 WP 仪表板中的“WordPress 地址”和“站点地址”都应设置为 HTTPS,而不是 HTTP。

#RewriteCond %{HTTP:X-Forwarded-SSL} !on
#RewriteCond %{REQUEST_URI} ^\/(images)
#RewriteRule (.*) https://%{HTTP_HOST}/$1 [L,R=301]

#RewriteCond %{HTTP:X-Forwarded-SSL} =on
#RewriteCond %{REQUEST_URI} !^\/(images)
#RewriteRule (.*) http://%{HTTP_HOST}/$1 [L,R=301]

RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI}

在根 .htaccess 文件中,您可以重定向到 HTTPSeverywhere,但如果请求 /images/ 目录(应保留为 HTTP 的目录),则例外以排除进一步处理。

例如:

# Prevent further processing if the "/images" directory is requested
RewriteRule ^images($|/) - [L]

# Redirect everything else to HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# BEGIN WordPress
:

无需重复 RewriteEngine On 指令,因为这已经在文件的后面(在 WP 部分中)出现。

确保在测试之前清除浏览器缓存并使用 302(临时)重定向进行测试以避免缓存问题。

这假设 SSL 证书已安装在您的应用服务器上,并且您没有使用可能正在管理 SSL 连接的代理/CDN/负载平衡器。


更新:由于上述情况仍会导致重定向循环,因此您可能位于管理 SSL 的某种代理服务器(CloudWays - 您的网络主机 - 或者可能是 Cloudflare)后面联系。对于第二条规则(HTTP 到 HTTPS 重定向)尝试以下操作:

# Redirect everything else to HTTPS
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

【讨论】:

  • 这给了我很多重定向
  • @GavinPedley 抱歉,应该在 substitution 字符串中使用 https://(而不是 http://)! (用 302s 测试的好理由!)我已经更新了我的答案。
  • Thanks 似乎仍然在图像文件夹以外的任何 URL 上提供了太多重定向。
  • 您是否已将 WordPress 更新为 HTTPS? (否则,WP 本身将重定向回 HTTP。)您是否在使用 Cloudflare(灵活 SSL)等 CDN 的代理/负载均衡器?
  • 我在 cloudways 上使用他们的基本 let 加密 SSL。不使用他们的 CDN。 DNS 通过 cloudflare,但不使用任何代理,仅使用 DNS。
猜你喜欢
  • 1970-01-01
  • 2010-11-06
  • 2023-03-29
  • 2015-09-29
  • 2010-10-29
  • 2017-08-13
  • 1970-01-01
  • 2014-01-05
  • 1970-01-01
相关资源
最近更新 更多