动态设置 RewriteBase 为当前文件夹路径 [英] Set RewriteBase to the current folder path dynamically
问题描述
有什么方法可以将 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 只使用复制和粘贴,而不更改 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 中写入:RewriteBase/
in this case I should write in the htaccess: RewriteBase /
如果我将 htaccess 移动到 sub_directory 中,我应该将 RewriteBase 更改为:
and if I move the htaccess inside sub_directory I should change RewriteBase 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
,即:
Then you can use %{ENV:BASE}
in your rules to denote RewriteBase
, i.e.:
#redirect in-existent files/calls to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . %{ENV:BASE}/index.php [L]
说明:
此规则的工作原理是将 REQUEST_URI
与 RewriteRule
看到的 URL 路径进行比较,即带有前导 RewriteBase 的
REQUEST_URI
被剥离了.区别在于 RewriteBase
并放入 %{ENV:BASE}
中.
Explanation:
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
OR%1
,%2
等但 RHS 端即条件字符串不能使用 这些$1
,$2
OR%1
,%2
变量. - 在 RHS 条件部分内部,我们只能使用内部反向引用,即我们在此条件中捕获的组.它们用
\1
、\2
等表示. - 在
RewriteCond
中,第一个捕获的组是(.*?/)
.它将由内部反向引用\1
表示. - 你可以看出,这条规则基本上是通过比较
%{REQUEST_URI}
和$1
来动态查找RewriteBase
.%{REQUEST_URI}
的示例将是/directory/foobar.php
并且$1
的示例对于同一示例 URI 将是foobar.php
.^(.*?/)(.*)::\2$
将差异放在第一个捕获组%1
或\1
.对于我们的示例,它将使用值/directory/
填充%1
和\1
,稍后将在设置环境变量时使用该值
.E=BASE:%1
中的 %{ENV:BASE}
- 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 make 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 befoobar.php
.^(.*?/)(.*)::\2$
is putting the difference in 1st captured group%1
or\1
. For our example it will populate%1
and\1
with the value/directory/
which is used later in setting up env variable%{ENV:BASE}
inE=BASE:%1
.
这篇关于动态设置 RewriteBase 为当前文件夹路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!