1

I have a set of apache (2.4.25) sites that use mod_vhost_alias so the one vhost services multiple subdomains.

I want to be able to set a environment variable, say

<VirtualHost _default_:443>`
ServerNameAlias *.whatever-it-is.com
SetEnv HOME_URL https://www.whatever-it-is.com/
# etc

then in my various sites .htaccess files pick up that variable. So a site might allow access to a subfolder or a specific file and redirect everything back to the home site.

php_flag engine off
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/?
RewriteCond %{REQUEST_URI} !^/myapp[/]?
RewriteCond %{REQUEST_URI} !^favicon.ico
RewriteRule (.*)  %{HOME_URL}  [R=301,L]
#                   ^--- not working, how would you do it?

then in the case where I need to change the home_url across all sites, I can just apply it in the vhost and each of the dozens of subdomain sites will pick it up.

* also tried

RewriteRule (.*)  %{ENV:HOME_URL}  [R=301,L]

http://httpd.apache.org/docs/2.0/env.html The %{ENV:variable} form of TestString in the RewriteCond allows mod_rewrite's rewrite engine to make decisions conditional on environment variables...

frumbert
  • 2,323
  • 5
  • 30
  • 61
  • `RewriteRule (.*) %{ENV:HOME_URL} [R=301,L]`? – Cyclonecode Mar 02 '17 at 07:26
  • Also tried `RewriteRule (.*) %{ENV:HOME_URL} [R,L]`, as per http://stackoverflow.com/a/7749784/1238884 - still not working. Ends up in a redirect loop, as if the environment variable isn't being set (which it is, as I can see it from php) – frumbert Mar 02 '17 at 07:55

1 Answers1

1

Use SetEnvIf to set your environment variable so that it is available for mod_rewrite later in .htaccess.

Inside VirtualHost config have it like this:

SetEnvIf Host ^ HOME_URL=https://www.whatever-it-is.com/

Then inside .htaccess have a simple rule:

RewriteEngine On

RewriteRule .* %{ENV:HOME_URL} [R=301,L]

Make sure to clear your browser cache before testing this change.

anubhava
  • 761,203
  • 64
  • 569
  • 643