Example should work but it doesn't

New to mod_rewrite? This is a good place to start.

Example should work but it doesn't

Postby marcelloma » Sun Sep 13, 2009 2:51 pm

I'm trying to run this example from the book "definite guide to mod rewrite"

Code: Select all
RewriteRule (.*) /index.php?$1 [PT]
RewriteCond $1 \.jpg$
RewriteRule . - [E=jpg:1]


The environmental variable jpg is not created..it is created though when using it in the RewriteRule
Code: Select all
RewriteRule (.*) /index.php?$1 [PT,E=jpg:1]


but it's not what I need..
it doesn't work either locally neither on my real hosting..any help?

Also why when the variable is created, locally is it called REDIRECT_jpg and on my host it's called REDIRECT_REDIRECT_jpg ?
marcelloma
 
Posts: 9
Joined: Sat Sep 12, 2009 4:14 am

Postby richardk » Sun Sep 13, 2009 4:45 pm

$1, used in the RewriteCond, is not set. It comes from the RewriteRule below the RewriteCond. Try
Code: Select all
RewriteRule (.*) /index.php?$1 [PT]
RewriteCond $1 \.jpg$
RewriteRule ^(.+)$ - [E=jpg:1]

It is pointless anyway as you could just do the test in the RewriteRule
Code: Select all
RewriteRule (.*) /index.php?$1 [PT]
RewriteRule \.jpg$ - [E=jpg:1]


The PT flag will not work (it does not need to) in .htaccess files.

Also why when the variable is created, locally is it called REDIRECT_jpg and on my host it's called REDIRECT_REDIRECT_jpg ?

The REDIRECT_ prefix is added each time mod_rewrite is processed, normally a .htaccess file mod_rewrite will be processed once: original request --> mod_rewrite rewrites --> new internal request --> mod_rewrite is checked but does not match --> go to file.

The request to /index.php?request.jpg matches the rule as well, so you get two or more matches: original request (/request.jpg) --> mod_rewrite rewrites --> new internal request (/index.php?request.jpg) --> mod_rewrite rewrites --> new internal request ... (it gets to it's limit) ... go to file.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby marcelloma » Mon Sep 14, 2009 1:10 am

Ok, well I tried
Code: Select all
RewriteRule (.*) /index.php?$1 [PT]
RewriteCond $1 \.jpg$
RewriteRule ^(.+)$ - [E=jpg:1]


but doesn't work either..

$1, used in the RewriteCond, is not set. It comes from the RewriteRule below the RewriteCond

Actually the book says
You
can use backreferences from the most recent RewriteRule, using the $N syntax, where N
is in the range 0–9


he request to /index.php?request.jpg matches the rule as well, so you get two or more matches: original request (/request.jpg) --> mod_rewrite rewrites --> new internal request (/index.php?request.jpg) --> mod_rewrite rewrites --> new internal request .

when there is the new internal request with index.php?request.jpg, shouldn't this rewrite Rule
Code: Select all
RewriteRule (.*) /index.php?$1 [PT]


redirect to /index.php?index.php?request.jpg? No, maybe not because the control doesn't start all over right?
marcelloma
 
Posts: 9
Joined: Sat Sep 12, 2009 4:14 am

Postby richardk » Mon Sep 14, 2009 10:13 am

Actually the book says
You
can use backreferences from the most recent RewriteRule, using the $N syntax, where N
is in the range 0–9

I'm right, they mean the same as me. To test
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^(abc.+)$ /def [R,L]
# Only continue if $1 is abc123 or ghi123
RewriteCond $1 ^(abc|ghi)123
RewriteRule ^(ghi.+)$ /jkl [R,L]

/abc456 redirects, /ghi456 does not. At the very least (if $1 is not being populated) all of /abc will redirect and none of /ghi will. RewriteConds are processed after the RewriteRule pattern (^(ghi.+)$) and before the substitution (/jkl).

he request to /index.php?request.jpg matches the rule as well, so you get two or more matches: original request (/request.jpg) --> mod_rewrite rewrites --> new internal request (/index.php?request.jpg) --> mod_rewrite rewrites --> new internal request .


when there is the new internal request with index.php?request.jpg, shouldn't this rewrite Rule
Code: Select all
RewriteRule (.*) /index.php?$1 [PT]


redirect to /index.php?index.php?request.jpg? No, maybe not because the control doesn't start all over right?

I think it would go to /index.php?index.php (the query string is not matched by the RewriteRule) and REDIRECT_jpg would be prefixed again to become REDIRECT_REDIRECT_jpg.

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule .\.jpg$ - [E=jpg:1]

# Don't match internal requests.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^.+$ /index.php?$0 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 26 guests

cron