![与 RewriteRule 匹配的请求返回权限被拒绝](https://linux22.com/image/204345/%E4%B8%8E%20RewriteRule%20%E5%8C%B9%E9%85%8D%E7%9A%84%E8%AF%B7%E6%B1%82%E8%BF%94%E5%9B%9E%E6%9D%83%E9%99%90%E8%A2%AB%E6%8B%92%E7%BB%9D.png)
我遇到了一个奇怪的 Apache 错误。我想重写所有来自 到/api/media/fi/le/path.jpg
的请求/media/fi/le/path.jpg
这是虚拟主机中设置的重写规则:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule /api/v1/* /src/api/v1/index.php [L]
RewriteRule /api/media/(.*) /media/$1 [L]
如果我将浏览器指向/media/fi/le/path.jpg
,图像将成功获取服务器。如果我去/api/media/fi/le/path.jpg
它会抱怨我没有访问该资源的权限。我必须获得许可,因为如果不进行重写,我就可以访问该文件。我应该如何重写最后一个 RewriteRule 以便它可以提供图像?
我添加了日志指令LogLevel alert rewrite:trace6
来查看 apache 在幕后做了什么。这是结果:
init rewrite engine with requested uri /api/media/32/ee/0e60731bcb220c7c7b.jpg
applying pattern '/api/v1/*' to uri '/api/media/32/ee/0e60731bcb220c7c7b.jpg'
applying pattern '/api/media/(.*)' to uri '/api/media/32/ee/0e60731bcb220c7c7b.jpg'
rewrite '/api/media/32/ee/0e60731bcb220c7c7b.jpg' -> '/media/32/ee/0e60731bcb220c7c7b.jpg'
local path result: /media/32/ee/0e60731bcb220c7c7b.jpg
go-ahead with /media/32/ee/0e60731bcb220c7c7b.jpg [OK]
我可以mydomain.com/media/32/ee/0e60731bcb220c7c7b.jpg
手动访问该网址,并且图像可以毫无问题地获取服务器。我缺少什么?
完整的.conf文件:
<IfModule mod_ssl.c>
<VirtualHost *:443>
DocumentRoot /home/user/project/src
ServerName project.com
ErrorLog logs/project-error_log
CustomLog logs/project-access_log common
DirectoryIndex index.php
<Directory /home/user/project/src>
Options -Indexes +FollowSymLinks
AllowOverride None
Require all granted
</Directory>
LogLevel alert rewrite:trace6
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule /api/v1/* /api/v1/index.php
RewriteRule /api/media/(.*) /media/$1 [L]
SSLCertificateFile /etc/letsencrypt/live/project.com/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/project.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateChainFile /etc/letsencrypt/live/project.com/chain.pem
</VirtualHost>
</IfModule>
答案1
重写引擎有奇怪的行为。
RewriteRule /api/media/(.*) /media/$1 [L]
永远禁止返回。mv media images
将上述指令更改为后RewriteRule /api/media/(.*) /images/$1 [L]
,权限错误就消失了。