Multiple Conditions, how to back reference

I’m learning mod rewrite and it seems when you do something like:

RewriteCond (reference)

That you can reference it in the next line with %1.

But when you have

RewriteCond (reference1)
RewriteCond (reference2)

If you make a reference in the next line, WANTING the reference from (reference1), using %1 instead gives you (reference2)

What do I need to do so I can get (reference1)? Do I have to set a variable?

This would likely be better understood if an example was provided.

I’d like to see what you’re doing with your RewriteRule and the method you are trying to use to backreference variables from your RewriteConds. It may be as simple as changing the %1 to a $1, however you have only confirmed that you know the syntax of RewriteCond, not the methods you are using for pattern matching.

You may want to take a look at for some beginner tips with using mod rewrite. Feel free to post back here if you have further detail that you think would help us help you.

I’ll give that a read right now. Here’s my code in question, FYI:

RewriteCond %{HTTP_HOST} ^(acp|clients|files|images|ssl|www)\.([-a-z0-9]+) [NC]
RewriteCond %{REQUEST_URI} \.(css|jpg|gif|png|js)$ [NC]
RewriteRule ^(.*)$ /%1/index\.cfm?good [L]

In the RewriteRule, I’m expecting %1 to capture “www” (in this case) but instead, it captures “css” in the request: http://www.aaronmartonecom/ui/css/file.css

This is a localhost request, I made a DNS entry that maps www.aaronmartonecom to localhost.


I supposed that could be interpreted a couple of different ways but your experience shows only one: Only the last RewriteCond’s atoms are retained for the RewriteRule (and it’s references cannot be "backreferenced by earlier RewriteCond statements). Of course, you can create an environmental variable with a [E=VAR:VAL] flag, too.



You’re trying to condense your rewrite too much. You can’t back-reference to the first RewriteCond (reference1), only the RewriteCond before (reference2).

I recommend keeping the first RewriteCond with the %{HTTP_HOST} and putting the ${REQUEST_URI} within the RewriteRule Test.

The RewriteRule would look something like…
RewriteCond %{HTTP_HOST} ^(acp|clients|files|images|ssl|www)\.([-a-z0-9]+) [NC]
RewriteRule ^/(.*).(css|jpg|gif|png|js)$ /%1/index\.cfm?good [L]

I hope that this is correct, I’m away from any servers for testing. Are you wanting to use the css, jpg, etc. extensions anywhere?

In short, I’m running locally right now, all the following point to localhost (see them as aliases):


The server directory root is: c:/inetpub/wwwroot
The site directory root is: c:/inetpub/wwwroot/aaronmartonecom

Inside the site directory I have 1 folder for each subdomain:

c:/inetpub/wwwroot/aaronmartonecom/files (for http://files.aaronmartonecom calls)
c:/inetpub/wwwroot/aaronmartonecom/www (for http://www.aaronmartonecom calls)

My current issue is that when the page code is created, if you’re familiar with HTML code, there is a BASE tag with an HREF attribute. I have one of 2 options:

1 <base href=“http://www.aaronmartonecom/” />
2 <base href=“http://www.aaronmartonecom/www/” />

I like option 1 better because it specifies the subdomain (which is the folder under the site root). With option 1, relative links like <img src=“ui/images/file.png”> would equal http://www.aaronmartonecom/ui/images/file.png. I would need the URL rewrite to see this URL and call “c:/inetpub/wwwroot/aaronmartonecom/www/ui/images/file.png” If I can do this, all is well. Links to pages like <a href=“about”>About</a> would goto http://www.aaronmartonecom/about" and the rewrite would determine that this doesn’t exist so it would request c:/inetpub/wwwroot/aaronmartonecom/www/index.cfm?$ses=/about

With option 2, the problem is that a relative link like <a href=“about”>About</a> would link then to http://www.aaronmartonecom/www/about (which is wrong, I want it to request http://www.aaronmartonecom/about, which would then call c:/inetpub/wwwroot/aaronmartonecom/www/index.cfm?$ses=/about

Sure, I could make sure that everyone of my links uses ABSOLUTE linking to avoid this, but I don’t want to settle for second place.