logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
Post new topic   Forum Index -> Apache View previous topic :: View next topic
Reply to topic   Topic: mod_rewrite: How to define a variable and use it in rules?
Author
jack01



Joined: 28 Feb 2014
Posts: 27

PostPosted: Fri 28 Mar '14 13:59    Post subject: mod_rewrite: How to define a variable and use it in rules? Reply with quote

Hi,
I am writing a rewrite rules using mod_rewrite module. I have the same data repeating all over the rules that I would like to replace with variable and set variable once at the top of rules then use variable in the rest of rules. Then if I need to add another IP address I would just add additional IP address to the variable instead like now need to change several rules.

For example I have IP addresses that I would like to set as a variable.

Now rules are the following (simplified) in httpd.conf:
**************************************************
RewriteEngine Off
RewriteCond %{REMOTE_ADDR} (192\.168\.5\.20|192\.168\.7\.15|10\.10\.20\.50)
RewriteRule (.*) - [R=404,L]

<many other conditions/rules with the same IP addresses>
**************************************************

Is there a way that I can set a variable like something:
**************************************************
RewriteEngine Off
SET_VARIABLE block_IP_addressees="192\.168\.5\.20|192\.168\.7\.15|10\.10\.20\.50"
RewriteCond %{REMOTE_ADDR} (block_IP_addresses)
RewriteRule (.*) - [R=404,L]

<many other conditions/rules with the same IP addresses>
**************************************************

My software:
- Apache httpd 2.4.27 from Apache Lounge installed on Windows 2008 R2 on Intel server

Thanks
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7298
Location: Germany, Next to Hamburg

PostPosted: Tue 29 Apr '14 15:59    Post subject: Reply with quote

Use SetEnv[1]


(code not tested)
Code:
RewriteEngine On
SetEnv block_IP_addressees="192\.168\.5\.20|192\.168\.7\.15|10\.10\.20\.50"
RewriteCond %{REMOTE_ADDR}=block_IP_addressees
RewriteRule (.*) - [R=404,L]





[1] http://httpd.apache.org/docs/2.4/mod/mod_env.html#setenv
Back to top


Reply to topic   Topic: mod_rewrite: How to define a variable and use it in rules? View previous topic :: View next topic
Post new topic   Forum Index -> Apache