Solved

mod-rewrite - Can I use a RewriteCond backreference in the PATTERN for the subsequent RewriteRule

Posted on 2006-06-29
2
1,717 Views
Last Modified: 2012-05-05
While this works as I'd expect (rewrites http://www.whatever.com/BLAH to http://www.whatever.com/test.php):

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/BLAH/ [NC]
RewriteRule ^BLAH/$ test.php [NC,L]

, I was half expecting this to work (both http://www.whatever.com/BLAH and http://www.whatever.com/SCHMLAH being rewritten to http://www.whatever.com/test.php):

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/(BLAH|SCHMLAH)/ [NC]
RewriteRule ^%1/$ test.php [NC,L]

I have tested that the backreference from the RewriteCond contains a value by tracing it in the substitution of the RewriteRule. It seems that i cannot use a backreference from a RewriteCond in the PATTERN of the RewriteRule.

Is this the case or am I doing something wrong?

Cheers
0
Comment
Question by:mikely
2 Comments
 
LVL 10

Accepted Solution

by:
sleep_furiously earned 125 total points
ID: 17013969
You are correct that you can't do that.

Let's establish some baseline terms:

RewriteCond TestString CondPattern
RewriteRule Pattern Substitution

According to the reference:
Using parentheses in 'Pattern' or in one of the 'CondPatterns' causes back-references to be internally created. These can later be referenced ... for creating the 'Substitution' and 'TestString' strings.

There's a diagram in the reference showing what backreferences are available at which points in the processing:
http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#InternalBackRefs

The red arrow in the lower right shows the bit related to your inquiry.  The backreference from the CondPattern is available in the Substitution, but not the Pattern.

0
 
LVL 8

Expert Comment

by:RWJDCom
ID: 17022244
I just found a real nice mod_rewrite cheat sheet that is real helpful when you are writing rules and conditions.  You can download it from http://www.ilovejackdaniels.com/apache/mod_rewrite-cheat-sheet/

I hope that Cheat Sheet can help you as much as it has helped me out.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

As Wikipedia explains 'robots.txt' as -- the robot exclusion standard, also known as the Robots Exclusion Protocol or robots.txt protocol, is a convention to prevent cooperating web spiders and other web robots from accessing all or part of a websit…
Over the last year I have answered a couple of basic URL rewriting questions several times so I thought I might as well have a stab at: explaining the basics, providing a few useful links and consolidating some of the most common queries into a sing…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now