Welcome to WebmasterWorld Guest from 54.198.245.233

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Multiple variables and multiple rewritecond

Keeping variables?

     
3:54 pm on Mar 24, 2006 (gmt 0)

Full Member

10+ Year Member

joined:July 8, 2005
posts:334
votes: 5


RewriteCond %{REQUEST_URI} ^/images/([0-9]+)
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.example\.com$
RewriteRule (.*) htp://www\.example\.com/view.php?user=%1&showimage=%2

(some of the contents of my .htaccess file. the htp:// is on purpose, to prevent WW making a link)

What I'm trying to do is to capture a variable in the first RewriteCond, another variable in the second RewriteCond and then use both variables in the RewriteRule. Weirdly enough, it's capturing the second variable, but not the first. And it's placing the second variable where the %1 is!

It's almost as if the first variable is being overwritten by the second rewritecond. I've swapped the rewritecond lines around and it still only captures the second variable.

Can any of the eminently bright minds at WW help me out? Thanks!

10:05 pm on Mar 24, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


The mod_rewrite documentation [httpd.apache.org] clearly specifies the behaviour you noticed; Only the variables from the last-matched RewriteCond are available as back-references.

You're lucky in that a simple re-arrangement of your code will fix it:


RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.example\.com
RewriteRule ^images/([0-9]+)$ http://www.example.com/view.php?user%1&showimage=$1

RewriteRule uses almost all of the %{REQUEST_URI} variable to compare against the pattern, so a separate RewriteCond isn't needed.

Another way to address the generic problem of accessing back-references to different server variables, although unnecessary in this case, would be something like:


RewriteCond %{REQUEST_URI}<>%{HTTP_HOST} ^www\.([^.]+)\.example\.com[^<]*<>/images/([0-9]+)
RewriteRule .* http://www.example.com/view.php?user=%2&showimage=%1

Note that the "<>" string has no special function whatsoever; It is used only as a fixed-text string to delimit the two variables so that the end of the first cannot be confused with the beginning of the second. Any sufficiently-unique characters could be used.

Other minor edits on pattern-anchoring and character-escaping were needed; The code shown above is as correct as I can make it, given my understanding of your code.

Jim

12:15 pm on Mar 25, 2006 (gmt 0)

Full Member

10+ Year Member

joined:July 8, 2005
posts:334
votes: 5


Thanks Jim, your help is much appreciated!
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members