0

I have this tree:

libs/
html/
app/
uploads/
    files/
    images/
        male.png
        female.png
    .htaccess
    download.php
.htaccess
index.php

/.htaccess has this content:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -Indexes
    </IfModule>

    RewriteEngine On

    RewriteRule ^ index.php [END,L]
</IfModule>

I want /uploads/.htaccess to:

  • redirect to download.php

    • if existent file, but not this .htaccess or download.php
  • redirect 404

    • if don't match existent file
    • or match .htaccess or download.php
    • or match existent folder

Examples:

www.example.com/uploads - redirect 404
www.example.com/uploads/files - redirect 404
www.example.com/uploads/images- redirect 404
www.example.com/uploads/.htaccess - redirect 404
www.example.com/uploads/download.php - redirect 404
www.example.com/uploads/nonexistentfile - redirect 404

www.example.com/uploads/images/male.php - redirect download.php
www.example.com/uploads/images/female.php - redirect download.php
Arșavin
  • 17
  • 6

2 Answers2

0

The discussion below should help you a bit: Redirect requests only if the file is not found?

# If requested resource exists as a file or directory go to it
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (.*) - [L]
#Alternatively the code above redirects where you want

# Else rewrite requests for non-existent resources to /404.php
RewriteRule (.*) /404.php?q=$1 [L]
0

I have asked the question also on serverfault.com and I got an expected answer: https://serverfault.com/questions/975415/if-file-exists-redirect-to-a-certain-file-else-redirect-to-404-from-htaccess/975451?noredirect=1#comment1269343_975451

Arșavin
  • 17
  • 6