Reputation: 7597
I am using this rewrite rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^img.domain.com [NC]
RewriteRule ^(.*)$ data/photos/$1 [L]
Its doing that, when I use f.e http://img.domain.com/profile_image.jpg
it will show content of file http://www.domain.com/data/photos/profile_image.jpg
without changing url (masked). This is ok.
The problem is, just in case, when somebody enter http://img.domain.com
, it will return 500 Internal server error, but I want it to show 403 Forbidden, the same as when you enter http://www.domain.com/data/photos/
. I am helpless, how to modify this rewrite rule to achieve this?
EDITED:
Also the same issue is when the file doesnt exist in /data/photos/
, then I want to return 404... but its returning 500.
In errorlog is too many recursions...
Upvotes: 1
Views: 40
Reputation: 785108
You should first check whether destination file exists and then rewrite it:
# 403 for img subdomain when request is for landing page
RewriteCond %{HTTP_HOST} ^img\.domain\.com$ [NC]
RewriteRule ^(data/photos)?/?$ - [F,NC]
RewriteCond %{HTTP_HOST} ^img\.domain\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/data/photos/$1 -f
RewriteRule ^(.+)$ data/photos/$1 [L]
Upvotes: 2