2

So I set an apache variable, and i'd like to use the value to set additional variables. This is possible, I tried various methods to get this to work

SetEnv URL_STATIC http://www.static.com

Doesn't work

SetEnv URL_STATIC_MORE %{ENV:URL_STATIC}/path/path

Doesn't work either :(

 SetEnv URL_STATIC_MORE ${URL_STATIC}/path/path

Ideally, i'd like URL_STATIC_MORE to be 'http://www.static.com/path/path' when all is said and done.

UPDATE:

The long winded reason i want to do this is to create a set of links that i can use in HTML/SSI. I Don't have php on my server, and that's not under my control.

So, i'd like to be able to use

<a href="<!--#echo var="ONE_VARIABLE_FOR_URL" -->">blah</a>

rather than

<a href="<!--#echo var="ROOT_URL" --><!--#echo var="PATH_URL" -->">blah</a>

Again, I do not have PHP available to me, and I have to do this for thousands of pages, so putting this in te .htaccess makes sense. I also have multiple root urls, so my config will eventually look like:

SetEnv URL_ROOT_CUSTSVC http://customerservice.company.com
SetEnv URL_ROOT_CORPORATE http://corp.company.com
SetEnv URL_ROOT_PRODUCTS http://products.company.com

SetEnv URL_ROOT_CUSTSVC_1 ${URL_ROOT_CUSTSVC}/path/1
SetEnv URL_ROOT_CUSTSVC_2 ${URL_ROOT_CUSTSVC}/path/2
SetEnv URL_ROOT_CUSTSVC_3 ${URL_ROOT_CUSTSVC}/path/3

SetEnv URL_ROOT_CORPORATE_1 ${URL_ROOT_CORPORATE}/path/1
SetEnv URL_ROOT_CORPORATE_2 ${URL_ROOT_CORPORATE}/path/2
SetEnv URL_ROOT_CORPORATE_3 ${URL_ROOT_CORPORATE}/path/3

SetEnv URL_ROOT_PRODUCTS_1 ${URL_ROOT_PRODUCTS}/path/1
SetEnv URL_ROOT_PRODUCTS_2 ${URL_ROOT_PRODUCTS}/path/2
SetEnv URL_ROOT_PRODUCTS_3 ${URL_ROOT_PRODUCTS}/path/3
Roy Rico
  • 602
  • 1
  • 7
  • 20
  • From your question, I'd say just `SetEnv URL_STATIC_MORE http://www.static.com/path/path`, but I suspect what you're trying to accomplish is more complicated than that? Can you expand a bit on what you're trying to do? – Shane Madden Aug 11 '11 at 02:04
  • http://serverfault.com/questions/235973/how-do-you-use-setenv-to-read-variables-in-apache – quanta Aug 11 '11 at 02:47
  • @quanta that question is close to what i'm trying to do, but I don't have PHP. i'm trying to reuse a variable i had set earlier in the config again, to expand and build URL's. – Roy Rico Aug 11 '11 at 03:17
  • You cannot: http://httpd.apache.org/docs/2.2/env.html#using – quanta Aug 11 '11 at 03:52
  • @Shane Madden, updated my question with more info, thanks for your time! – Roy Rico Aug 11 '11 at 03:52
  • @quanta thanks. u wanna put that as an answer, so i can mark it? – Roy Rico Aug 11 '11 at 03:53

2 Answers2

2

Environment variables only work with the following Apache modules:

They cannot be called with SetEnv because that is a directive of the mod_env module.

More information: http://httpd.apache.org/docs/2.2/env.html#using

quanta
  • 50,327
  • 19
  • 152
  • 213
0

There seems to be a workaround using mod_rewrite, though the specifics depend on whether you need all three domains' variables set for all requests, or only the variables for the current domain:

RewriteEngine on

RewriteCond %{HTTP_HOST} ^(customerservice.company.com)$
RewriteRule .* - [E=URL_ROOT:http://%1,E=URL_PATH_1:http://%1/custserv/path/1,E=URL_PATH_2:http://%1/custserv/path/2,E=URL_PATH_3:http://%1/custserv/path/3]

RewriteCond %{HTTP_HOST} ^(corp.company.com)$
RewriteRule .* - [E=URL_ROOT:http://%1,E=URL_PATH_1:http://%1/corp/path/1,E=URL_PATH_2:http://%1/corp/path/2,E=URL_PATH_3:http://%1/corp/path/3]

RewriteCond %{HTTP_HOST} ^(products.company.com)$
RewriteRule .* - [E=URL_ROOT:http://%1,E=URL_PATH_1:http://%1/products/path/1,E=URL_PATH_2:http://%1/products/path/2,E=URL_PATH_3:http://%1/products/path/3]

If you need all of the variables set for every page, regardless of the request domain, just swap %{HTTP_HOST} for the hostname in question, and rename your variables accordingly:

RewriteEngine on

RewriteCond customerservice.company.com ^(customerservice.company.com)$
RewriteRule .* - [E=URL_ROOT_CUSTSVC:http://%1,E=URL_PATH_CUSTSVC_1:http://%1/custserv/path/1,E=URL_PATH_CUSTSVC_2:http://%1/custserv/path/2,E=URL_PATH_CUSTSVC_3:http://%1/custserv/path/3]

RewriteCond corp.company.com ^(corp.company.com)$
RewriteRule .* - [E=URL_ROOT_CORPORATE:http://%1,E=URL_PATH_CORPORATE_1:http://%1/corp/path/1,E=URL_PATH_CORPORATE_2:http://%1/corp/path/2,E=URL_PATH_CORPORATE_3:http://%1/corp/path/3]

RewriteCond products.company.com ^(products.company.com)$
RewriteRule .* - [E=URL_ROOT_PRODUCTS:http://%1,E=URL_PATH_PRODUCTS_1:http://%1/products/path/1,E=URL_PATH_PRODUCTS_2:http://%1/products/path/2,E=URL_PATH_PRODUCTS_3:http://%1/products/path/3]

Unfortunately, this isn't the most readable way to set variables according to the values of other variables. If you need readability, that will unfortunately require scripting of some sort (PHP, perl, or perhaps even shell, if you have that kind of access - which you've already said you do not).

But hey, if readability isn't an issue, this should work just fine!