Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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


Doesn't work either :(


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


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



share|improve this question
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 2:04
serverfault.com/questions/235973/… –  quanta Aug 11 '11 at 2: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 3:17
You cannot: httpd.apache.org/docs/2.2/env.html#using –  quanta Aug 11 '11 at 3:52
@Shane Madden, updated my question with more info, thanks for your time! –  Roy Rico Aug 11 '11 at 3:52

2 Answers 2

up vote 2 down vote accepted

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

share|improve this answer

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!

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.