Solved

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

Posted on 2006-06-29
2
1,718 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
URL rewriting - LAMP 7 43
Zipping many files freeze my whole server through php 5 62
Virtual host in apache 31 94
Can't connect to WAMP server 5 54
Hi, in this article I'm going to teach you how to run your own site, and how to let people in (without IP). I'll talk about and explain each step... :) By the way, everything in this Tutorial is completely free and legal. This article is for …
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

825 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