I'm trying to create a service that you give it a URL like http://myhost.com/34.png and it will return the result of the script http://myhost.com/create_image.php?count=34.

I know this is possible with mod_rewrite, but I'm very new to this and I'd like some pointers on how to do this. I only want it to work for numbers.

1 Answer 1


ServerFault's Everything You Ever Wanted to Know about Mod_Rewrite Rules but Were Afraid to Ask topic is a great start - but, more to the point, you're looking for something like this:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} ^/([0-9]*)\.png$
RewriteRule ([0-9]*)\.png$ /create_image.php?count=$1 [L]

Line 1: Enable the mod_rewrite engine (the mod_rewrite Apache module may need to be installed and enabled separately of this directive if it is not already active - i.e. if "RewriteEngine on" throws an error)

Line 2: If (and only if) the request file path begins with a leading slash and is composed of a numeric string (of arbitrary length) followed by the ".png" extension (note use of \ character for escaping) then...

Line 3: Set the variable $1 to the numeric string and pass the request on to the create_image.php file with the count parameter set accordingly then stop (L = last request)

I highly recommend the AskApache mod_rewrite tutorials if you're just picking up mod_rewrite - I believe you'll find it's pretty easy to get the hang of it if you're accustomed to writing scripts.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.