WordPress 上传文件夹中带有 URL 编码字符的图像返回 404s

Images with URL encoded characters in WordPress uploads folder returning 404s

我的 WordPress 上传文件夹中有很多图片。大多数这些图像显示得很好。但是,当我尝试通过网络浏览器访问它们时,任何文件名中包含 URL 编码字符的图像(例如 %22 而不是 ") return 都是 404。我已通过 FTP 连接、下载其中一张有问题的图片并在我的计算机上打开它来验证这些图片文件存在于服务器上。

我想这可能是一个 .htaccess 问题,但 .htaccess 对我来说是难以理解的。这是我站点根目录中的 .htaccess

# BEGIN rlrssslReallySimpleSSL rsssl_version[3.3.5]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} !=on [NC]
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/
RewriteRule ^(.*)$ https://%{HTTP_HOST}/ [R=301,L]
</IfModule>
# END rlrssslReallySimpleSSL
# 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

# Wordfence 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 Wordfence WAF

这是我的 /wp-content/uploads/ 文件夹中的 .htaccess 文件:

# BEGIN Wordfence code execution protection
<IfModule mod_php5.c>
php_flag engine 0
</IfModule>
<IfModule mod_php7.c>
php_flag engine 0
</IfModule>
<IfModule mod_php.c>
php_flag engine 0
</IfModule>

AddHandler cgi-script .php .phtml .php3 .pl .py .jsp .asp .htm .shtml .sh .cgi
Options -ExecCGI
# END Wordfence code execution protection

我的图像路径中没有其他 .htaccess 文件。我觉得我快疯了,但我可能遗漏了一些明显的东西。

这似乎是来自客户端的初始 HTTP 请求的 URL 编码问题,与您的 .htaccess 文件无关。如果实际文件名包含 %22,则需要将其 URL 编码为 %2522(即 %22 中的 % 也需要进行 % 编码)在请求中,否则,它将查找包含 " 而不是 %22 的文件(当请求的 URL 被解码时)。

这不是您的 .htaccess 文件的问题,尽管您可以在 .htaccess 的帮助下解决这个问题(手动 URL 编码这些请求),但仅如果您无法“修复”HTML 源中请求的 URL。在源代码中修复这些 URL 是首选解决方案。

不用说,首先要避免文件名中出现特殊字符(即清理上传的文件名)。双引号 (") 是 Windows OS 文件名中的无效字符,所以这可能就是它在文件名中保持 % 编码的原因?但除此之外,% 编码的字符在 filenames 中没有位置,因为这是 URL 编码方案。