Webmasters Stack Exchange is a question and answer site for pro webmasters. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 4 down vote accepted

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.

share|improve this answer
Thanks! Just what I needed. I'll check the tutorials. – Juliana Peña Jan 8 '11 at 23:17

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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