Php htaccess反向目录
可以让htaccess查找与url相关的特定文件,如果找不到,则返回一步 例如: /example/here/where/from /示例/此处/何处/来源 Htaccess检查“/example/here/where/from”是否确实是某种类型的文件(/example/here/where/from.php),如果失败,则返回一步并检查“/example/here/where”是否是文件(/example/here/where.php)等等 如果必须返回到树中,则假定它们为参数,如中所示: /example/here.php $_GET['params']='where/from' 提前谢谢 编辑:Php htaccess反向目录,php,apache,.htaccess,redirect,Php,Apache,.htaccess,Redirect,可以让htaccess查找与url相关的特定文件,如果找不到,则返回一步 例如: /example/here/where/from /示例/此处/何处/来源 Htaccess检查“/example/here/where/from”是否确实是某种类型的文件(/example/here/where/from.php),如果失败,则返回一步并检查“/example/here/where”是否是文件(/example/here/where.php)等等 如果必须返回到树中,则假定它们为参数,如中所示:
拼写这是一个非常棘手的问题,但是这里有一个递归遍历给定请求的父目录的代码,它支持无限深度 通过
httpd.conf
启用mod_rewrite和.htaccess,然后将此代码放入文档根目录下的.htaccess
:
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
# If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
# don't do anything
RewriteRule ^ - [L]
# if current ${REQUEST_URI}.php is not a file then
# forward to the parent directory of current REQUEST_URI
RewriteCond %{DOCUMENT_ROOT}/$1/$2.php !-f
RewriteRule ^(.*?)/([^/]+)/?$ $1/ [L]
# if current ${REQUEST_URI}.php is a valid file then
# load it be removing optional trailing slash
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
说明:
假设原始URI是:/index/foo/bar/baz
。另外,假设%{DOCUMENT\u ROOT}/index.php
存在,但DOCUMENT\u ROOT
下不存在其他php文件
重写规则#1有一个正则表达式,它将当前请求#URI分为两部分:
除了最低的细分到$1
之外,其他所有的细分都将在这里成为索引/foo/bar
最低细分为$2
,此处为baz
RewriteCond检查%{DOCUMENT\u ROOT}/$1/$2.php
(这意味着%{DOCUMENT\u ROOT}/index/foo/bar/baz.php
)是否为无效文件
如果条件成功,则会在内部重定向到$1/
,即索引/foo/bar/
重写规则#1的逻辑将再次重复,以使请求#URI为(在每次递归之后):
index/foo/bar/
index/foo/
index/
此时,规则#1的RewriteCond失败,因为那里存在${DOCUMENT_ROOT}/index.php
如果%{DOCUMENT\u ROOT}/$1.php
是一个有效文件,我的重写规则#2说转发到$1.php
。请注意,RewriteRule#2的正则表达式与除最后一个斜杠之外的所有内容都匹配,并将其放入$1
。这意味着检查%{DOCUMENT\u ROOT}/index.php
是否为有效文件(确实是)
此时,mod_rewrite处理已完成,因为无法触发进一步的规则,因为这两个RewriteCond在此之后都失败,因此,%{DOCUMENT_ROOT}/index.php
会得到Apache web服务器的适当服务。这是固定深度的(例如,在您的示例中,它最多有4个项目深度),或者你想处理无限深度吗?我对递归逻辑的理解太累了,我跳过了解释部分,现在添加:)@john:添加了一些解释。谢谢,这个问题的答案,你能看一下吗?[问题]:很好的回答,我一直在寻找这样的东西,很长时间了。虽然我不得不做一些调整来为我工作:1。删除“重写基/”2。Switch:RewriteCond%{DOCUMENT\u ROOT}/$1.php-f用于RewriteCond%{REQUEST\u FILENAME}.php-f第二个修复方法是使用没有.php的页面,自动添加它。例如:www.somesime.com/home.php/a/b/c www.somesime.com/home/a/b/c两者都可以工作。