2

I want to make reference to a variable called "foo" (with value "bar")

SetEnv foo bar

within a RewriteRule

 RewriteRule ^([^/]*)/([^/]*)/$ /{foo}.php?p=$1&id=$2 [L]

so that it literally reads as:

RewriteRule ^([^/]*)/([^/]*)/$ /bar.php?p=$1&id=$2 [L]

How is this possible?

anubhava
  • 761,203
  • 64
  • 569
  • 643
p_mcp
  • 2,643
  • 8
  • 36
  • 75
  • 1
    possible duplicate of [Using custom environment variables in .htaccess](http://stackoverflow.com/questions/3627411/using-custom-environment-variables-in-htaccess) – Alex W Apr 07 '14 at 18:18

1 Answers1

1

Tricky but can be done by combining mod_setenvif and mod_rewrite.

Use this code in your root .htaccess:

RewriteEngine On

SetEnvIf Host ^ foo=bar

RewriteRule ^([^/]+)/([^/]+)/?$ /%{ENV:foo}.php?p=$1&id=$2 [L]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Can you explain what "SetEnvIf Host ^" does please? As I would like to conditionally set foo=bar on a given domain name. Eg. If domain1.com, set foo=bar1, else if domain2.com = foo=bar2 – p_mcp Apr 07 '14 at 18:33
  • That is indeed possible with `SetEnvIf Host`. At present I just compare that with `^` (line start anchor) which means always set. But you can do: `SetEnvIf Host ^domain1 foo=domain1` OR `SetEnvIf Host ^domain2 foo=domain2` etc. – anubhava Apr 07 '14 at 18:36