如果没有找到文件/文件夹mod-rewrite目录 [英] mod rewrite directory if file/folder not found

查看:112
本文介绍了如果没有找到文件/文件夹mod-rewrite目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为文件夹1的文件夹在我的根目录

www.domain.com/ www.domain.com/folder1

我需要重定向所有的请求www.domain.com该转起来是一个404错误,以FOLDER1。像这样:

www.domain.com/a_file.txt

如果a_file.txt不存在,在文件夹1看:

www.domain.com/folder1/a_file.txt

我想这对子目录的工作原理相同,像这样:

www.domain.com/a_folder (重定​​向如果没有根存在)

www.domain.com/folder1/a_folder

我知道我应该使用的RewriteCond%{REQUEST_FILE}!-f,但我似乎无法找到答案。

解决方案

  RewriteEngine叙述上
的RewriteBase /
的RewriteCond%{REQUEST_URI}!^ /文件夹1 /
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则(。*)文件夹1 / $ 1 [L,R]
 

  • 在第一重写康德确保您不会循环(如果该文件不里面的文件夹1存在任何
  • 第二个将检查目标不是一个文件
  • 第三 - 它不是一个文件夹或者
  • 最后,重写URL。 标志意味着这是最后加的规则(即使有后的规则),研究表示重定向。您还可以添加 QSA 标记,如果你想传递给原来的发送到新的URL
  • 的任何查询字符串参数

I have a folder named "folder1" in my root directory

www.domain.com/ www.domain.com/folder1

I need to redirect all the requests to www.domain.com that turn up to be a 404 error, to folder1. Like so:

www.domain.com/a_file.txt

If a_file.txt doesn't exist, look in folder1:

www.domain.com/folder1/a_file.txt

I want this to work the same for subdirectories, like so:

www.domain.com/a_folder (redirect if it doesn't exist in the root)

www.domain.com/folder1/a_folder

I know I should use RewriteCond %{REQUEST_FILE} !-f but I can't seem to figure it out.

解决方案

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/folder1/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) folder1/$1 [L,R]

  • The first rewrite-cond ensures you do not loop (in case the file does not exist inside folder1 either
  • The second one checks that target is not a file
  • The third - that it is not a folder either
  • And, finally, rewrite the url. L flag means this is the last rule applied (even if there are rules after it), R means redirect. You can also add QSA flag if you want any query-string parameters passed to the original be sent to the new url

这篇关于如果没有找到文件/文件夹mod-rewrite目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆