Mike L.
Mike L.

Reputation: 1966

Variables in .htaccess file

Is there a way to store and retrieve variables throughout the entire htaccess file. All the searching I do just shows how to create a variable with RegEx through mod_rewrite for applying it to a query string. This is not what I am trying to do. I am implementing a version control system for a multi-application project. Current project versions are stored in a folder structure like : product/applications/application/version There will be multiple applications in the applications folder and multiple versions within each application folder such as: v1.0, v1.1, v2.0, etc.

One of the things I am trying to accomplish is using htaccess for version control (I am already using it to rewrite urls for applications, I just want the version number in the example below it is 0.1 to be a variable defined at the top of my htaccess file. Is that possible?

RewriteEngine On
Options +FollowSymlinks
# version here is 0.1 and I would want it 
# defined as $apiVersion if possible
# it is the same for both of these rewrites.
RewriteRule ^api/json$ /malvern/api/api/v0.1/public/api.php?format=json [NC,L]
RewriteRule ^api/xml$ /malvern/api/api/v0.1/public/api.php?format=xml [NC,L]

# version here is also 0.1 but would be
# a differnt variable than the first
RewriteRule ^srsmith/rate$ /malvern/webship/webship/v0.1/apps/ratepackage/public/index.php [NC,QSA,L]

Upvotes: 4

Views: 4710

Answers (1)

Jon Lin
Jon Lin

Reputation: 143906

Try using the E flag in mod_rewrite. At the very top of your rules, add a:

RewriteRule .* - [E=VERSION:v1.0]

or whatever the version you want, then whenever you want to use your version, do this:

RewriteRule ^api/json$ /malvern/api/api/%{ENV:VERSION}/public/api.php?format=json [NC,L]
RewriteRule ^api/xml$ /malvern/api/api/%{ENV:VERSION}/public/api.php?format=xml [NC,L]

Upvotes: 8

Related Questions