htaccess的反向目录 [英] htaccess reverse directory

查看:318
本文介绍了htaccess的反向目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是可以让htaccess的查找与URL中的特定文件,并退一万步,如果不是发现了什么?

It is possible to let htaccess look for a specific file related to the url and go back one step if not found?

例如:


/example/here/where/from

.htaccess的外观,如果'/例子/位置/在哪里/从的确是某种(/example/here/where/from.php)的文件,如果失败则返回一步,并检查是否''/例如,/在这里/那里是一个文件(/example/here/where.php)等。

Htaccess looks if the '/example/here/where/from' is indeed a file of some sort(/example/here/where/from.php) if this fails it goes back one step and checks if ' '/example/here/where' is a file(/example/here/where.php) and so forth.

如果它不得不回去树认为它们作为参数,如下所示:

If it had to go back in the tree assume them as parameters as in:

/example/here.php

/example/here.php

$ _ GET ['参数'] ='在那里/从';

$_GET['params'] = 'where/from';

在此先感谢

编辑:

拼写

推荐答案

这是非常棘手,但这里是一个code表示递归遍历给定REQUEST_URI的父目录,它支持无限深度。

This is very tricky but here is a code that recursively traverse to parent dir of the given REQUEST_URI and it supports infinite depth.

的httpd.conf 启用了mod_rewrite和.htaccess,然后把这个code在的.htaccess DOCUMENT_ROOT 目录:

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:

# 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是: /索引/富/酒吧/巴兹。也让我们说,%{DOCUMENT_ROOT}的index.php 存在,但在 DOCUMENT_ROOT 不存在其他PHP文件。

Explanation:

Let's say original URI is: /index/foo/bar/baz. Also let's say that %{DOCUMENT_ROOT}/index.php exists but no other php files exist under DOCUMENT_ROOT.

重写规则#1有一个正则表达式是打破当前REQUEST_URI分为两部分:

RewriteRule #1 has a regex that is breaking current REQUEST_URI into 2 parts:

  1. 所有,但最低的子目录到 $ 1 将在指数/富/酒吧此处
  2. 最低子目录到 $ 2 这将是巴兹此处
  1. All but lowest subdir into $1 which will be index/foo/bar here
  2. Lowest subdir into $2 which will be baz here

的RewriteCond检查是否%{DOCUMENT_ROOT} / $ 1 / $ 2.PHP (这相当于是否%{DOCUMENT_ROOT} /指数/富/酒吧/baz.php )的不会的有效文件。

RewriteCond checks whether %{DOCUMENT_ROOT}/$1/$2.php (which translates to whether %{DOCUMENT_ROOT}/index/foo/bar/baz.php) is NOT a valid file.

如果条件成功,那么它在内部被重定向到 $ 1 / 指数/富/酒吧/ 在这里。

If condition succeeds then it is internally redirected to $1/ which is index/foo/bar/ here.

重写规则#1的逻辑是重复再次让REQUEST_URI为(后每个递归):

RewriteRule #1's logic is repeated again to make REQUEST_URI as (after each recursion):

  1. 指数/富/酒吧/
  2. 指数/富/
  3. 指数/
  1. index/foo/bar/
  2. index/foo/
  3. index/

在这一点上的RewriteCond失败规则#1,因为 $ {DOCUMENT_ROOT}的index.php 存在那里。

At this point RewriteCond fails for rule # 1 because ${DOCUMENT_ROOT}/index.php exists there.

我重写规则#2表示期待 $ 1.PHP 如果%{DOCUMENT_ROOT} / $ 1.PHP 是一个有效的文件。需要注意的是重写规则#2具有正则表达式匹配的一切,但最后一个斜线,并将其放入 $ 1 。这意味着检查是否%{DOCUMENT_ROOT}的index.php 是一个有效的文件(它的确是)。

My RewriteRule #2 says forward to $1.php if %{DOCUMENT_ROOT}/$1.php is a valid file. Note that RewriteRule #2 has regex that matches everything but last slash and puts it into $1. This means checking whether %{DOCUMENT_ROOT}/index.php is a valid file (and it indeed is).

在这一点上的mod_rewrite处理完毕,由于没有进一步的规则可以被解雇,因为两者的RewriteCond失败后,因此%{DOCUMENT_ROOT}的index.php 被正式通过服务您的Apache Web服务器。

At this point mod_rewrite processing is complete as no further rule can be fired since both RewriteCond fails after that, therefore %{DOCUMENT_ROOT}/index.php gets duly served by your Apache web server.

这篇关于htaccess的反向目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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