?
Solved

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

Posted on 2006-06-29
2
Medium Priority
?
1,725 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 10

Accepted Solution

by:
sleep_furiously earned 500 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If your site has a few sections that need to be secure when data is transmitted between the server and local computer, such as a /order/ section for ordering or /customer/ which contains customer data, etc it would of course be recommended to secure…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month8 days, 1 hour left to enroll

765 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