1

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?

Community
  • 1
  • 1
pleasega
  • 543
  • 1
  • 3
  • 21
  • `RewriteCond %{DOCUMENT_ROOT}/sub/imgs/$1 -f RewriteRule ^.+/(.+\.jpeg?|png|gif)$ /redirect.html [L]` – Amit Verma Mar 26 '16 at 14:13
  • @starkeen hi it doesn't work for me, I edited the question to fix some directory errors. i think the way I entered the directories were wrong just now. – pleasega Mar 26 '16 at 14:19

1 Answers1

1

You can try this rule in sub/.htaccess:

RewriteEngine On
RewriteBase /sub/

RewriteCond %{DOCUMENT_ROOT}/sub/imgs/$1 -f
RewriteRule ^(.+\.(?:jpe?g|png|gif|tiff|ico))$ success.html [L,NC]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Hi, can you breakdown to me what the last line does? The `(.+\.(?:jpe?g|png|gif|tiff|ico))` – pleasega Mar 26 '16 at 14:58
  • Last line matches any image name that ends with any of these extensions `jpe?g|png|gif|tiff|ico`. `(?:..)` is called non-capturing group – anubhava Mar 26 '16 at 15:22
  • Thanks! I read http://stackoverflow.com/questions/3512471/what-is-a-non-capturing-group but still don't understand what is it used for here? – pleasega Mar 26 '16 at 15:34
  • 1
    We could also use `(jpe?g|png|gif|tiff|ico)` in this rule but that is called a capturing group as it puts image extension in `$2. However here we don't want to capture it so using a non-capturing group. – anubhava Mar 26 '16 at 16:33
  • May I know what happens if we use a capture group here and how is it done? I changed the html to php and readfile an image but the image doesn't display, instead it displays the gremlins, not sure why this happened or how to fix it. I have already put the Content type header. – pleasega Mar 27 '16 at 10:30
  • @pleasega: I suggest opening a new question for image display issue with more details. – anubhava Mar 27 '16 at 10:43