I have tried to figure this out for very long but still cant. I have tried htaccess get filename without path but it doesn't work for me.
Here is my directory
/www
|-- /other.org
|-- /example.com
|-- /sub
|-- .htaccess
|-- success.html
|-- /imgs
|-- photo.png
I am trying to find if an image exists in http://example.com/sub/imgs when there is a request for that image or file at http://example.com/sub. If it exists, then user will be redirected to success.html. But I cannot do that.
For example if I type into the browser
http://example.com/sub/photo.png
It checks if the file photo.png or any kind of image exists in /sub/imgs/photo.png, it does not exist and should not exist in /sub directory.
http://example.com/sub/imgs/photo.png
The redirected output will be success.html if it exists.
http://example.com/sub/success.html
I tried
RewriteCond %{DOCUMENT_ROOT}/sub%{REQUEST_URI} -f
RewriteRule (.*)\.(gif|jpg|jpeg|png)$ success.html
but the %{REQUEST_URI}
is /sub/photo.png
so the output will be http://example.com/sub/imgs/sub/photo.png
. There isn't any request that can get the file name or anything after the last slash (/). Is it possible to get just the file name photo.png
so I can just do:
RewriteCond %{DOCUMENT_ROOT}/sub/imgs%{FILE_NAME} -f
RewriteRule (.*)\.(gif|jpg|jpeg|png)$ success.html
Is there any way to get this working?