homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Regex with Apache RewriteRule

 6:29 pm on Aug 3, 2011 (gmt 0)

Hello Folks....

How you doing all ...

Need your assistance with an Regex with apache rewriterule.

This is what am doing...

1. Using Apache webserver as proxy server for multiple tomcats
2. Using IP filter with in apache using RewriteCondition and RewriteRule
3. Trying to specify in RewriteRule using a regex to look for a specific pattern "ZVZZT" and then if matches redirect to a page.

RewriteCond %{REMOTE_ADDR} !=
RewriteCond %{REMOTE_ADDR} !=
RewriteRule /app1/([A-Z_a-z\.?&=]*)(ZVZZT) /var/www/htdocs/index.html [R=301]

It works fine if a new request comes in as /app1/ZVZZT [ it does redirect to index.html as permanant .. no back button works in browser]. However if the request comes in from already bookmarked link such as


In this case my regex doesnt work. Kindly assist.

Highly appreciated!




 7:20 pm on Aug 3, 2011 (gmt 0)

Do a search within this Forum for "query string". The query is not part of the rewrite pattern.

:: wandering off to compose boilerplate, as I see I will need it ::


 7:31 pm on Aug 3, 2011 (gmt 0)

You should not redirect to a URL with index.php in the new URL.

In this case, you should not be externally redirecting at all.

You should compose an internal rewrite to fetch the content at the originally requested URL.

Replace [R=301] with [L] here.

However, that's not the only problem here.

The RewriteRule pattern can look ONLY at the PATH part of the URL request. In your example URL that doesn't work, the ZVZZT part is in the query string. Your rule and RegEx pattern requires for it to be in the path. If you want to test for it in the query string then you need to add another rule. In that new rule you will also add a preceding RewriteCond that tests %{QUERY_STRING} for this pattern.


 8:13 pm on Aug 3, 2011 (gmt 0)

Thanks for the replies ...

g1smd - I 've changed as per your suggestion

RewriteCond %{REMOTE_ADDR} !=
RewriteCond %{REMOTE_ADDR} !=
RewriteCond %{QUERY_STRING} /appl/([A-Z_a-z\.?&=]*)(ZVZZT)
RewriteRule ^(.*)$ /var/www/htdocs/index.html [R=301,L]

Still no luck... please correct wherever necessary.

Thank you!



 9:12 pm on Aug 3, 2011 (gmt 0)

RewriteCond %{QUERY_STRING} /appl/([A-Z_a-z\.?&=]*)(ZVZZT)

This is not a query string. The query string is the part AFTER the question mark. You want simply


Since all you're looking for is a query string that contains a particular sequence of letters, you don't need anchors.

Oh, and you can collapse the first two conditions to

RewriteCond %{REMOTE_ADDR} !=192\.168\.10\.[25]$

Here you must include an ending anchor to avoid forms such as ".51" or ".232", and you must escape literal periods. A request for "" is not a request for, let's say, "192.168.1005" and therefore the condition can succeed when you want it to fail.


 3:32 pm on Aug 4, 2011 (gmt 0)

Very much thanks Lucy .. i had this part in mind too about Query String .. but the later one is new one and pretty good ... niiice

However, i will let little more about my scenario...

1. we got app1 and app2 hosted in 2 different tomcats on the same box
2. Both apps serve multiple databases based on the identifier i.e.in this case ZVZZT

URL for the first application will be
* www.abc.om/app1/ZVZZT

URL for the second application will be

* www.abc.com/app2/ZVZZT

Now, i've to restrict only specific IP's for one application of ZVZZT not both of them . i.e. block "app1" requests from certain IPs for ZVZZT.

Considering this, i cannot simply put ZVZZT as QUERY STRING. Kindly advice....

Thanks again....


 5:07 pm on Aug 4, 2011 (gmt 0)

Lucy .... just to inform .. i 've specified 2 rules.... and this works...

RewriteCond %{REMOTE_ADDR} !^10\.42\.15\.14$
RewriteCond %{REMOTE_ADDR} !^10\.42\.15\.16$
RewriteRule /app1/ZVZZT\z /var/www/htdocs/index.html [R=301]

RewriteCond %{REMOTE_ADDR} !^10\.42\.15\.14$
RewriteCond %{REMOTE_ADDR} !^10\.42\.15\.16$
RewriteRule /app1/([A-Z_a-z\.?&=]*)$ [google.com...] [R=301]

Thanks to your tip... highly appreciated....



 8:07 pm on Aug 4, 2011 (gmt 0)

Meaning: if anyone other than the two named Good Guys asks for /app1/ plus ZVZZT, send them somewhere else? Got it.

RewriteRule /app1/ZVZZT\z /var/www/htdocs/index.html [R=301]

What's the \z ?

RewriteRule /app1/([A-Z_a-z\.?&=]*)$

You can leave out the ?&= here, since they will never occur in the path. In fact, since you're not capturing anything, just looking for ZVZZT in the query string, all you need in the "pattern" is /app1/ without anchors.


 9:12 pm on Aug 4, 2011 (gmt 0)

Lucy - As i said in my first post i want both conditions to be satisfied, hence two different rewriterules....

\z --- Matches at the end of the string the regex pattern is applied to.




 10:15 pm on Aug 4, 2011 (gmt 0)

Yes, I spent some time trying to figure out how to collapse them into a single rule, but it would have involved using both AND and OR. I'm not going there :)
\z --- Matches at the end of the string

Apache likes $. Yes, that means the dollar sign does two entirely different and unrelated jobs. Don't look at me ;) But unless ZVZZT has to be the very end of the request, it doesn't need to be anchored.


 11:10 pm on Aug 4, 2011 (gmt 0)

Add the L flag to every rule.

Why are you redirecting to a different URL?

In order to get round your rules I need merely ask for example.com/index.html.

You should be using a rewrite here.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved