如果存在文件,则重定向到某个文件,否则从 htaccess 重定向到 404
If existing file, redirect to a certain file, else redirect to 404 from htaccess
我有这棵树:
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
下面的讨论应该对您有所帮助:
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= [L]
我也在 serverfault.com 上问过这个问题,我得到了预期的答案:
https://serverfault.com/questions/975415/if-file-exists-redirect-to-a-certain-file-else-redirect-to-404-from-htaccess/975451?noredirect=1#comment1269343_975451
我有这棵树:
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
下面的讨论应该对您有所帮助: 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= [L]
我也在 serverfault.com 上问过这个问题,我得到了预期的答案: https://serverfault.com/questions/975415/if-file-exists-redirect-to-a-certain-file-else-redirect-to-404-from-htaccess/975451?noredirect=1#comment1269343_975451