如何将所有子文件夹和目录重定向到主根除文件?

The*_*Kid 4 .htaccess mod-rewrite redirect

基本上我有一个CDN设置.

/public_html/
index.html

/files/
image/
video/
video2/video2
Run Code Online (Sandbox Code Playgroud)

我想将所有子文件夹和目录重定向到主文件夹 - 除了文件扩展名,所以如果有人加载文件,我希望加载,这样人们就无法查看目录.(我不想看到404或禁止的消息,只是希望目录转到主页)

这将使我免于上传index.php重定向.这可以用.htaccess完成吗?

Boo*_*eus 6

# don't show any files in the folder
IndexIgnore *

ErrorDocument 403 /
ErrorDocument 404 /

RewriteEngine On

# disabled user to access directly to the files folder.
RewriteRule ^files(\/?)$ - [F]

# images
RewriteRule ^image/(.*)\.(jpg|png|jpeg|gif)$ /files/$1.$2 [L]
# video
RewriteRule ^video/(.*)\.(flv|avi|mov)$ /files/$1.$2 [L]
#etc...
Run Code Online (Sandbox Code Playgroud)

如果您有子文件夹,(.*)它将自动使用它.例:

http://www.domain.com/image/i.png => /files/i.png

http://www.domain.com/image/sub1/sub2/i.png => /files/sub1/sub2/i.png
Run Code Online (Sandbox Code Playgroud)

这里有一些链接可以帮助您:

http://www.javascriptkit.com/howto/htaccess.shtml

http://www.htaccess-guide.com/

http://net.tutsplus.com/tutorials/other/the-ultimate-guide-to-htaccess-files/

http://www.bloghash.com/2006/11/beginners-guide-to-htaccess-file-with-examples/

  • 当然,查看我的更改 (6认同)