HellaMad
HellaMad

Reputation: 5374

How to use .htaccess to rewrite requests from image to PHP script?

I have a PHP script located at http://sb1.dev.codeanywhere.net/~a70097sb/hc/onlinestatus/image.php that requires two GET variables: ign and style. My .htaccess file is in the same directory as image.php.

I want requests of the form http://sb1.dev.codeanywhere.net/~a70097sb/hc/onlinestatus/{style}/{ign}.png to be rewritten to http://sb1.dev.codeanywhere.net/~a70097sb/hc/onlinestatus/image.php?style={style}&ign={ign}. How do I do this, as I have no experience with mod_rewrite, and wasn't able to get it to work using any of the tutorials I found on Google.

Tried this but it didn't work, I just get a 404 page:

Options +FollowSymlinks -MultiViews
RewriteEngine on

RewriteRule ^([^.]*)/([^.]*)\.png$ image.php?style=$1&ign=$2 [L,NC]

Upvotes: 1

Views: 1050

Answers (2)

Michael Berkowski
Michael Berkowski

Reputation: 270599

You're on the right track. The capture pattern ([^/]+) matches everything up to the next /, so you'll need two of those.

RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)\.png$ image.php?style=$1&ign=$2 [L,NC]

Upvotes: 3

golja
golja

Reputation: 1093

Try something like that:

<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteCond %{HTTP_HOST} ^www\.mysite\.com [NC]
   RewriteRule ^hc\/onlinestatus\/(.*)\/(.*)\.png /hc/onlinestatus/image.php?style=$1&ign=$2 [NC,L]
</IfModule>

Upvotes: 0

Related Questions