设置的RewriteBase到当前文件夹路径动态 [英] Set RewriteBase to the current folder path dynamically

查看:1196
本文介绍了设置的RewriteBase到当前文件夹路径动态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法来设置的RewriteBase到路径当前文件夹的相对(其中.htaccess文件中的文件夹)到主机根?

Is there any way to set RewriteBase to the path current folder (the folder which the .htaccess file is in) relative to the host root?

我有一个CMS,如果我将它移动到目录中,我的主机不工作,除非我设置的RewriteBase相对于主机的根目录的路径。我想我的CMS只复制​​和粘贴工作,在不改变任何code htaccess的。

I have a CMS and if I move it to the directory in my host it does not work unless I set the RewriteBase to the path of directory relative to the root of host. I would like my CMS to work with only copy and paste, without changing any code in htaccess.

例如:

webroot

 - sub_directory
 - cms
 - .htaccess

在这种情况下,我应该写在htaccess的: RwriteBase /

in this case I should write in the htaccess: RwriteBase /

如果我移动htaccess的内部sub_directory我应该改变RwriteBase为:

and if I move the htaccess inside sub_directory I should change RwriteBase to:

的RewriteBase / sub_directory /

所以,我想是这样

的RewriteBase /%{current_folder} /

推荐答案

下面是可以抢的的RewriteBase 中的环境变量,然后你就可以在用一种方式你其他重写规则:

Here is one way one can grab the RewriteBase in an environment variable which you can then use in your other rewrite rules:

RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=BASE:%1]

然后你可以用%{ENV:BASE} 在规则表示的RewriteBase

本规则适用的 REQUEST_URI 比较的URL路径重写规则认为,这是 REQUEST_URI 与领先的的RewriteBase 剥夺。所不同的是的RewriteBase 和投入%{ENV:BASE}

This rule works by comparing the REQUEST_URI to the URL path that RewriteRule sees, which is the REQUEST_URI with the leading RewriteBase stripped away. The difference is the RewriteBase and is put into %{ENV:BASE}.

  • 在一个的RewriteCond 时,LHS(测试字符串)可以使用反向引用变量如 $ 1 $ 2 %1 %2 等,但RHS侧即条件字符串的不能使用这些 $ 1 $ 2 %1 %2 变量。
  • 在RHS条件部分只能向后引用,我们可以使用的内部的反向引用即群体,我们已抓获在这种情况本身。他们表示为 \ 1 \ 2
  • 的RewriteCond 第一个捕获组(。*?/)。这将重新通过内部反向引用psented $ P $`\ 1。
  • 正如你可以标记出来,这个规则基本上是找到的RewriteBase 动态比较%{REQUEST_URI} $ 1 。对%{REQUEST_URI} /directory/foobar.php 的例子,例如$ 1 的同一个例子URI将 foobar.php ^(。*?/)(。*):: \ 2 $ 正在建立第一个捕获组比较差异%1 \ 1 。在本例中,将填充%1 \ 1 的值 /目录/ 这是在建立ENV变量%{BASE} E = BASE以后使用:%1
  • In a RewriteCond, the LHS (test string) can use back-reference variables e.g. $1, $2 OR %1, %2 etc but RHS side i.e. condition string cannot use these $1, $2 OR %1, %2 variables.
  • Inside the RHS condition part only back-reference we can use are internal back-references i.e. the groups we have captured in this condition itself. They are denoted by \1, \2 etc.
  • In the RewriteCond first captured group is (.*?/). It will be represented by internal back-reference `\1.
  • As you can mark out that this rule is basically finding RewriteBase dynamically by comparing %{REQUEST_URI} and $1. An example of %{REQUEST_URI} will be /directory/foobar.php and example of $1 for same example URI will be foobar.php. ^(.*?/)(.*)::\2$ is putting the difference in 1st captured group %1 or \1. For our example it will populate %1 or \1 with the value /directory/ which is used later in setting up env variable %{BASE} i.e. E=BASE:%1.

这篇关于设置的RewriteBase到当前文件夹路径动态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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