【问题标题】:If existing file, redirect to a certain file, else redirect to 404 from htaccess如果存在文件,则重定向到某个文件,否则从 htaccess 重定向到 404
【发布时间】:2019-11-23 08:52:15
【问题描述】:

我有这棵树:

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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 我想要相反的。如果找到文件,则重定向到 download.php。
    【解决方案2】:
    猜你喜欢
    • 2015-03-18
    • 2011-09-12
    • 1970-01-01
    • 2015-09-23
    • 2011-03-01
    • 2014-08-02
    • 1970-01-01
    • 1970-01-01
    • 2011-12-25
    相关资源
    最近更新 更多