首页 > 解决方案 > WordPress 中带有 URL 编码字符的图像上传文件夹返回 404

问题描述

我的 WordPress 上传文件夹中有很多图片。这些图像中的大多数显示都很好。但是,当我尝试通过网络浏览器访问它们时,任何文件名中包含 URL 编码字符(例如,%22而不是)的图像都会返回 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}/$1 [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

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

# 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我的图像路径上没有其他文件。我觉得我快疯了,但我很可能错过了一些明显的东西。

标签: wordpress.htaccessuploadhttp-status-code-404urlencode

解决方案


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

这不是您的.htaccess文件的问题,尽管您可以在.htaccess(手动对这些请求进行 URL 编码)的帮助下解决此问题,但前提是您无法“修复”HTML 源中请求的 URL。从源头修复这些 URL 将是首选解决方案。

不用说,首先避免文件名中的特殊字符(即清理上传的文件名)将是首选。双引号 ( ") 是 Windows 操作系统文件名中的无效字符,所以这可能就是为什么它在文件名中保持 % 编码?但除此之外, %-encoded 字符在filenames中没有位置,因为这是一个URL编码方案。


推荐阅读