Is there anyway to use the value set in a SetEnv
on the RewriteRule
statement?
Example:
SetEnv MY_SCRIPT myScript.php
rewriteEngine on
RewriteRule ^(.*)$ %{MY_SCRIPT} [L]
There are two main directive of this module: RewriteCond & RewriteRule . RewriteRule is used to rewrite the url as the name signifies if all the conditions defined in RewriteCond are matching. One or more RewriteCond can precede a RewriteRule directive.
In your rewrite, the ^ signifies the start of the string, the (. *) says to match anything, and the $ signifies the end of the string. So, basically, it's saying grab everything from the start to the end of the string and assign that value to $1.
The mod_rewrite module uses a rule-based rewriting engine, based on a PCRE regular-expression parser, to rewrite requested URLs on the fly. By default, mod_rewrite maps a URL to a filesystem path. However, it can also be used to redirect one URL to another URL, or to invoke an internal proxy fetch.
RewriteRule specifies the directive. pattern is a regular expression that matches the desired string from the URL, which is what the viewer types in the browser. substitution is the path to the actual URL, i.e. the path of the file Apache servers. flags are optional parameters that can modify how the rule works.
According to http://httpd.apache.org/docs/2.0/env.html the SetEnv is called after the RewriteRule. Therefore it seems to be impossible to use any variable set via SetEnv in a RewriteRule- or RewriteCond-statement.
Using SetEnvIf on the other hand is called before the RewriteRule and therefore Variables set there can be used in a RewriteRule- or RewriteCond-statement.
So something like the following should work:
SetEnvIf SERVER_PROTOCOL "HTTP.*" MY_SCRIPT=myScript.php
rewriteEngine on
RewriteRule ^(.*)$ %{ENV:MY_SCRIPT} [L]
Use %{ENV:MY_SCRIPT}
instead of %{MY_SCRIPT}
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With