AardvarkBusiness.net - Business Search Engine AardvarkBusiness.net - Business Search Engine



 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

     

mod_rewrite & .htaccess custom variables for Apache

 
 
Post new topic   Reply to topic    AardvarkBusiness.net Forum Index -> Programming Forum
View previous topic :: View next topic  
Author Message
trev
President
President


Joined: 15 Oct 2002
Posts: 3011
Location: NW England, UK
4037 ants

PostPosted: Fri Sep 04, 2009 3:51 pm    Post subject: mod_rewrite & .htaccess custom variables for Apache Reply with quote

Something that took me months to work out when using mod_rewrite in .htaccess was how to store information and reuse it over and over again, ie: create custom variables in .htaccess

There seem to be masses of hints that this is possible, but very little actual substance, until I finally figured it by combining some of the hints with lots of testing. So I thought it might be useful to add a note about how to do this.


You create mod_rewrite variables thus:

Code:
RewriteCond  %{REQUEST_URI}  ^/category_abc/
RewriteRule .* - [E=cat_id:1]

RewriteCond  %{REQUEST_URI}  ^/category_def/
RewriteRule .* - [E=cat_id:2]


- The E= tells Apache we're creating a new ENV variable.
- The cat_id is the name of the variable we're creating
- The :x is the value of the variable (simple key : value syntax).


Now an example of using the custom environment variable:

Code:
RewriteRule ^category_[a-z]+$   outputscript.php?cat_id=%{ENV:cat_id}


If the URL comes through as say www.example.com/category_abc/ then internally we'll call "outputscript.php?cat_id=1".

Obviously things can get a lot more complicated than this but just remember to use the syntax %{ENV:variable_name} to pickup the value of the ENV variable and you should be fine. Those are curly brackets btw.

Hopefully that will help save someone months of frustration and hassle. If used properly it can reduce your .htaccess files by a huge amount.

Trev
_________________
Mad Theories - Ambleside - Coniston - Grange
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    AardvarkBusiness.net Forum Index -> Programming Forum All times are GMT + 1 Hour
Page 1 of 1

 
Google
 
Business Forum Sport Forum Travel Forum


Powered by php B B © 2001, 2002 php B B Group

AardvarkBusiness.net Business Search Engine & Directory