【问题标题】:How do I redirect all Sub folders and directories to the main root excluding files?如何将所有子文件夹和目录重定向到不包括文件的主根目录?
【发布时间】:2011-09-23 10:37:20
【问题描述】:

基本上我有一个 CDN 设置。

/public_html/
index.html

/files/
image/
video/
video2/video2

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

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

【问题讨论】:

  • 是的,您的 .htaccess 是什么样的?
  • 我没有做任何事情...只是删除了 WWW
  • 所以你想要:/video.mpeg(某人在 url 中访问的文件)-> /media/video.mpeg(实际文件)?
  • 没有。基本上 www.site.com/files/video.flv 是人们可以查看的网址。我不希望人们看到 www.site.com/files 并查看我希望重定向到 www.site.com 的所有文件- 问题是我也有很多子文件夹...

标签: .htaccess mod-rewrite redirect


【解决方案1】:
# 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...

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

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

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

这里有一些对您有帮助的链接:

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/

【讨论】:

  • 如果我把这个放在我的 URL 中会发生什么:domain.com/image/sub1 或这个domain.com/image/sub1/sub2
  • 来自 /image/sub1/ 文件夹的标准 404。我建议使用自定义的 404 和 403 消息。这个是 404,当有人直接访问 /files/ 时是 403(查看我更新的答案)
  • 我们不能将所有 404 和 403 重定向到根目录吗?
  • 当然,查看我的更改
  • 非常感谢,您可以推荐任何链接以了解 htaccess 规则吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-23
  • 2017-01-29
  • 2021-09-03
  • 2011-01-19
  • 2015-07-20
相关资源
最近更新 更多