Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mod_rewrite rule and setenv

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]
like image 351
H. Ferrence Avatar asked Nov 23 '09 20:11

H. Ferrence


People also ask

What is RewriteCond and RewriteRule?

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.

What is $1 rewrite rule?

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.

What is mod_rewrite in Apache?

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.

What is Apache rewrite rules?

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.


2 Answers

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]
like image 131
heiglandreas Avatar answered Sep 22 '22 13:09

heiglandreas


Use %{ENV:MY_SCRIPT} instead of %{MY_SCRIPT}.

like image 26
Gumbo Avatar answered Sep 24 '22 13:09

Gumbo