如果存在文件,则重定向到某个文件,否则从htaccess重定向到404

时间:2019-07-14 10:09:08

标签: .htaccess redirect mod-rewrite http-status-code-404

我有这棵树:

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

/。htaccess具有以下内容:

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

    RewriteEngine On

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

我希望/uploads/.htaccess访问:

  • 重定向到download.php

    • 如果存在文件,但不是此.htaccess或download.php
  • 重定向404

    • 如果不匹配现有文件
    • 或匹配.htaccess或download.php
    • 或匹配现有文件夹

示例:

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

2 个答案:

答案 0 :(得分:0)

下面的讨论应该对您有所帮助: 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]

答案 1 :(得分:0)