문제

in a project i have .htaccess files in many Subdirectories.

here is an example Structure:

project/.htaccess

project/admin/.htaccess

project/admin/pdf/.htaccess

in the last one i successfully use the following:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /project/admin/pdf/
RewriteRule ^([pdf\.actions]+)$ pdf.out.actions.php [QSA]

this works fine, if the Project Folder is placed directly in the root directory. all other subdirs have same htaccess logic, now if the user needs to put the project in any other directory rather than root, he/she has to change all RewriteBase in all htaccess Files in all subdirectories of the project.

my question: how to rewrite my htaccess content, so i get the same result as mentioned above but with "dynamic" base or without Base...etc? i tried somany things without success.

i removed the line RewriteBase /project/admin/pdf/ and tested it, i get site not found...etc!

any Idea?

thanks

도움이 되었습니까?

해결책

You can try adding something like this to the top of your rules:

RewriteCond %{ENV:URI} ^$
RewriteRule ^(.*)$ - [ENV=URI:$1]

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

And then instead of using RewriteBase, you'll have to include the BASE environment variable everywhere. For example:

RewriteRule ^([pdf\.actions]+)$ %{ENV:BASE}/pdf.out.actions.php [QSA]

The conditions do a couple of things, but both rules change nothing in the URI and only add environment variables.

The first condition is necessary because it grabs the requested URI before anything is done to it, and stores it. This is important because the (.*) grouping in the rule has the base stripped off. We want the unaltered URI with the base stripped off. So the URI environment variable is the URI with the base stripped off.

The second condition is necessary because it compares the URI environment variable with the %{REQUEST_URI}, which is the entire URI, including the base. That comparison yields us the part that was stripped off, or the base, and that'll be stored in the BASE environment variable.

The conditions which match against ^$ is simply ensuring that this is the first time through the rules (meaning neither of the environment variables have been set). The rewrite engine will loop so we only want to set these the first time.


EDIT: actually, now that I'm looking at it, you could probably leave the first one out:

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

Using the $1 backreference which matches the rule, noting that the rule itself is partly evaluated (pattern applied) before any of the conditions are checked.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top