Solved

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

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

Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

Question has a verified solution.

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

If you've heard about htaccess and it sounds like it does what you want, but you're not sure how it works... well, you're in the right place. Read on. Some Basics #1. It's a file and its filename is .htaccess (yes, with a dot in the front). #…
In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

691 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