Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
528 views
in Technique[技术] by (71.8m points)

regex - Set RewriteBase to the current folder path dynamically

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?

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.

Update:

For example:

webroot

 - sub_directory
 - cms
 - .htaccess

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

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

RewriteBase /sub_directory/

So I want something like

RewriteBase /%{current_folder}/

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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]

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]

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}.

  • 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 be foobar.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} in E=BASE:%1.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...