Forum Moderators: phranque
/software/games/foo/graphics/foo.gif
to:
/graphics/software/games/600/foo_01.gif
So while this would work:
RewriteRule ^software/games/([a-z0-9]+)/graphics/([a-z0-9]+).gif$ /graphics/software/games/600/$1_01.gif
It would also match things like:
/software/games/foo/graphics/bar.gif
/software/games/foo/graphics/zzz.gif
/software/games/foo/graphics/yyy.gif
And so on.
When I tried to replace the second ([a-z0-9]+) with a back reference within the pattern, it didn't work. (I was trying to use \1 as a back reference, but I could be wrong about this).
I suspect that either I'm mixing up the syntax for back references within the RewriteRule pattern or that I should be using a RewriteCondition (which I've unsuccessfully tried).
Any suggestions?
Thanks!
/software/games/foo/graphics/foo.gif
Not sure I understand exactly... If you are only trying to redirect one file, then just use that file like this:
RewriteRule ^software/games/foo/graphics/foo.gif$ /graphics/software/games/600/foo_01.gif [L]
If you are trying to match more, or do something different, please post some more specifics.
Hope this helps.
Justin
BTW Adding 'no case' and 'last' [NC,L] should make your rules more solid like this:
RewriteRule ^software/games/([a-z0-9]+)/graphics/([a-z0-9]+).gif$ /graphics/software/games/600/$1_01.gif [NC,L]
/software/games/foo/graphics/foo.gif
/software/games/bar/graphics/bar.gif
/software/games/yyy/graphics/yyy.gif
/software/games/zzz/graphics/zzz.gif
should internally redirect to:
/graphics/software/games/600/foo_01.gif
/graphics/software/games/600/bar_01.gif
/graphics/software/games/600/yyy_01.gif
/graphics/software/games/600/zzz_01.gif
I'm definitely going to be adding flags and optimizing things, but I'm still trying to get past the problem of ensuring two groups in a pattern are identical.
Thanks!
RewriteRule ^software/games/([a-z]+)/graphics/[a-z]+\.gif$ /graphics/software/games/600/$1_01.gif [L]
Justin
I think I am just going to keep adding, because my first answer sucked... You can do the comparrison with a condition, but I think it would be much more efficient without. This may need a 'little tweeking' but it's close:
RewriteCond %{REQUEST_URI} ^/$1$2\.gif
RewriteRule ^(software/games/([a-z]+)/graphics/)[a-z]+\.gif$ /graphics/software/games/600/$2_01.gif [L]
This breaks down as follows...
(software/games/([a-z]+)/graphics/) = stores the complete string in $1
([a-z]+) = stores the specific directory in $2
RewriteCond %{REQUEST_URI} ^/ = Copmares the requested URI to $1 'software/games/directory/graphics/' $2 'directory' .gif IOW
software/games/foo/graphics/foo.gif will match, but
software/games/foo/graphics/dog.gif will not
You can elimate the \.gif and leave the line open (no $) to be more efficient, if you do not have any other files that will qualify... this is an implicit 'and everything else'
Like this:
RewriteCond %{REQUEST_URI} ^/$1$2
- I think that's it this time. =)
And I can't see any reason why this part:
^/$1$2\.gif
wouldn't work in theory, but I'm still too unsure about how to use backreferences in rewrite conditions to know either way.
Any thoughts?
Thanks again!
The right side of a RewriteCond must be a string. You cannot compare two variables with RewriteCond. In the absense of POSIX 1003.2, you can't comapre two variables at all in mod_rewrite, so another method must be used.
If you do have access to atomic back-references, compare are possible, but difficult. The trick is to remember that if A+B is equal to A+A, then A and B must be equal. In mod_rewrite terms using atomic back-references, that translates to this equality test:
RewriteCond %1<>%2 ^(.*)<>\1$
A negative compare is then:
RewriteCond %1<>%2 !^(.*)<>\1$
A simpler way to do this is to rewrite your incoming URLs to a unique path that you can test for statically, in order to prevent looping.
Jim