首页 > 解决方案 > 当我在 HTTPS (.htaccess) 上访问一个没有结束斜杠的文件夹时出现奇怪的行为

问题描述

我有一个托管在真实主机上的网站(使用Apache 2.4.46Php 7.4(64 位)托管 linux),以及在我的 PC 上托管的网站(使用Apache 2.4.48(OpenSSL 1.1.1k)和Php 8.07(64 位)托管 Windows 10)。

首先我必须说,在真正的托管(托管 linux)上,我必须使用不同的.htaccess配置:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

在我的电脑(托管窗口)上,我必须使用另一种.htaccess配置:

RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

所以我将两种配置组合成一个规则集:

################################################################################
########################### Fix for Loading Resources ##########################
################################################################################
Options -Multiviews
################################################################################
############################### Force to use HTTPS #############################
################################################################################
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
################################################################################
########################## Set Security Header Options #########################
################################################################################
<IfModule mod_headers.c>
  Header always set Content-Security-Policy "upgrade-insecure-requests;"
  Header set X-Content-Type-Options "nosniff"
  Header append X-Frame-Options "SAMEORIGIN"
  Header set X-XSS-Protection "1; mode=block"
  Header unset X-Powered-By
</IfModule>
################################################################################
########################## Prevent Caching for CSS/JS ##########################
################################################################################
<FilesMatch "\.(?i:css|js)$">
  <IfModule mod_expires.c>
    ExpiresActive Off
  </IfModule>
  FileETag None
  <IfModule mod_headers.c>
    Header unset ETag
    Header unset Pragma
    Header unset Cache-Control
    Header unset Last-Modified
    Header set Pragma "no-cache"
    Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
    Header set Expires "Thu, 1 Jan 1970 00:00:00 GMT"
  </IfModule>
</FilesMatch>

在我的 PC ( localhost) 上,当我访问文件夹时"gallery"没有结束斜杠时,我可以看到:

GET https://localhost/gallery [HTTP/2 301 Moved Permanently 0ms]

GET https://localhost/gallery/ [HTTP/2 302 Found 201ms]

在我的真实主机上,当我访问"gallery"没有结束斜线的文件夹时,我可以看到:

GET https://www.example.it/gallery [HTTP/2 301 Moved Permanently 116ms]

GET http://www.example.it/gallery/ [HTTP/1.1 301 Moved Permanently 73ms]

GET https://www.example.it/gallery/ [HTTP/2 302 Found 84ms]

如您所见(在firefox consolle(CTRL + Shift + I)中),在localhost我将所有记录标记为HTTP/2时,在真实主机上我将一条记录标记为HTTP/1。这意味着没有HTTPS

我没有.htaccess内部文件夹"gallery"或其他文件夹,除了root我发布的.htaccess.

我能做些什么来修复它?我想要一个.htaccess适用于两个系统并适用于所有文件夹的规则,HTTP/2即使没有结尾斜杠,我也可以完全访问所有文件夹。

PS:在我拥有HTTP/2SSL(真HTTPS)的两个系统上。

谢谢。

标签: apache.htaccessmod-rewritehttpshttp2

解决方案


推荐阅读