我正在用PHP开发一个相当简单的文件浏览器。
文件位于/ home / web / www / archive
与归档下的相应文件匹配的请求是/ files
/files/sub/file.txt => /home/web/www/archive/sub/file.txt (dump as is)
/files/file.png => /home/web/www/archive/file.png (dump as is)
/files => /home/web/www/archive (directory, handled by /home/web/www/index.php)
/files/sub => /home/web/www/archive/sub (directory, handled by /home/web/www/index.php)
我希望我的.htaccess能够识别它是否是归档文件/,如果是,只需按原样输出文件。如果没有发送到index.php。
答案 0 :(得分:1)
您可以使用-f
标志:
RewriteEngine On
# if the request is for /archive..
RewriteCond %{REQUEST_URI} ^\/archive\/
# and the request is not for a file
RewriteCond %{REQUEST_FILENAME} !-f
# send to index.php
RewriteRule .* index.php [L]
请注意,这也会重定向到index.php以获取对不存在文件的文件请求。您可以为目录RewriteCond %{REQUEST_FILENAME} -d
添加肯定检查,因此只有在您请求目录时才会重定向。
Documentation in the apache docs with all flags like -f
, -d
, ...
答案 1 :(得分:0)
.htaccess中的以下内容会将任何非文件的内容发送到您的index.php
RewriteCond %{REQUEST_URI} !\/files\/(.*)\.[a-z]+$
RewriteRule .* index.php
因此,任何没有文件名终止(.jpg,.bat,.txt,.etc)的请求都将转到您的索引。
已更新,现在应该可以使用了。
答案 2 :(得分:0)
RewriteCond %{REQUEST_URI} ^/files/(.*)$
RewriteCond %{DOCUMENT_ROOT}/archive/%1 -f
RewriteRule .* %{DOCUMENT_ROOT}/archive/%1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]