designermonkey
designermonkey

Reputation: 1108

Apache SetEnvIf and using %{ENV:...}

My question is related to the use of SetEnvIf and how I can then use the environment variables correctly as I need them. I shall explain what I need to do, then what I already have...

I need to set the php application directory in my htaccess file, as some of the rewrite rules use that directory name, and I'm trying to allow the admin user the ability to change it. It is currently set in the php, and changing it is done manually in the htaccess file and then again in the php. To achieve this, I currently have the following (taking advice regarding using env vars in rewrite rules)

SetEnvIf SERVER_PROTOCOL ".*" APP_DIR=application

It's set up that way to get the env var set before any rewrite rules, as SetEnv is processed after any rewrite rules, whereas SetEnvIf is processed before.

Now I can access APP_DIR using %{ENV:APP_DIR} in rewrite conditions, and the second part of any rewrite rules.

What I need to achieve is a little more complex than that, as the application directory doesn't exist, but is rather a redirect to let the PHP application know to process as a back-end rather than a front-end, for example the rewrites currently (without the env var) are

### ADMIN REWRITE
RewriteRule ^application\/?$ index.php?mode=administration&%{QUERY_STRING} [NC,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^application(\/(.*\/?))?$ index.php?page=$1&mode=administration&%{QUERY_STRING} [NC,L]

### FRONTEND REWRITE - Will ignore files and folders
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*\/?)$ index.php?page=$1&%{QUERY_STRING} [L]

So, as you can probably see, I need to be able to substitute application for APP_DIR to let the administrators of the system change the path, so it could be cms or admin etc.

Sadly, the following won't work

### ADMIN REWRITE
RewriteRule ^%{ENV:APP_DIR}\/?$ index.php?mode=administration&%{QUERY_STRING} [NC,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^%{ENV:APP_DIR}(\/(.*\/?))?$ index.php?page=$1&mode=administration&%{QUERY_STRING}  [NC,L]

### FRONTEND REWRITE - Will ignore files and folders
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*\/?)$ index.php?page=$1&%{QUERY_STRING} [L]

%{ENV:APP_DIR} cannot be used in the regex of a rewrite, as the regex is processed before any rewrites, and the env vars.

Has anyone ever achieved what I need? Can it be done, even if it means adjusting the rewrite logic somewhat? I must state that the php logic after the rewrites can't be changed as it's a long time existing system which would have backwards compatibility issues if it were.

Upvotes: 2

Views: 3603

Answers (1)

Zenexer
Zenexer

Reputation: 19611

Use RewriteCond to match %{REQUEST_URI} with the environment variable up to the first slash (/), then start your RewriteRule regex with ^[^/]*/. That will work if you're not dealing with subdirectories; otherwise, you'll have to forego a definitive starting point in your RewriteRule regexes.

Upvotes: 1

Related Questions