在提供Apache错误页面时,如何隐藏所请求资产的文件路径?

时间:2016-03-01 03:25:54

标签: apache .htaccess mod-rewrite permissions

我们的服务器使用以下重写规则来模糊我们的图片位置,这会将www.domain.com/media/*定向到www.domain.com/secret/sub/directory/to/images/

RewriteRule ^media/(.*) /secret/sub/directory/to/images/$1 

这在提供有效图像时效果很好,但是当请求无效资产URL时,404通知会显示完整的真实文件路径而不是用户友好的URL。 (我也注意到它也会发生在403s)。

例如:

Not Found

The requested URL /secret/sub/directory/to/images/image.png was not found on this server.

Apache/2.2.22 (Ubuntu) Server at www.domain.com Port 443

如何说服服务器显示面向用户的URL而不是完整的文件路径?

1 个答案:

答案 0 :(得分:2)

请尝试使用以下规则集:

RewriteEngine on

# If the file requested exists in the images directory
# then serve it as if it were in media.
RewriteCond %{REQUEST_URI} ^/media/(.+) [NC]
RewriteCond %{DOCUMENT_ROOT}/secret/sub/directory/to/images/%1 -f
RewriteRule ^ /secret/sub/directory/to/images/%1 [L]

第一个条件检查我们是否从media请求,如果是,则将该文件名传递给下一个条件,该条件检查我们请求的内容是否实际存在于秘密目录中。

如果两个条件都通过,则使用对第一个条件的捕获部分的反向引用((.+) => %1)将请求内部重写到秘密目录。

如果秘密目录中不存在请求的文件,则跳过内部重写,Apache将显示服务器上不存在/media/non-existent.png