mod_rewrite for apache

I had a written some code to generate dynamic images on our website. I soon discovered that my images were not being listed in Google’s image section.
The following URL generates the required image:

https://www.somewhereincanada.com/barcode/barcode.php?code=001600064300&mode=gif

However, when I tried to save it to my desktop it saves it as barcode.php.png . Google will not list it in it’s image section as ‘barcode.php?code=001600064300&mode=gif’ is not a valid name for an image file.

One solution was to have my script save the generated image to my server. Then I could redirect web browser’s to the generated image by returning the text:

META HTTP-EQUIV=”Refresh” CONTENT=”0; URL=/barcode/images/001600064300.gif”

But by doing that I am using up valuable space on our web host.

It would be nice if I could have the URL:

https://www.somewhereincanada.com/barcode/images/001600064300.gif

call my script and dynamically generate the image.

This is possible by using Apache’s Module mod_rewrite.

By using mod_rewrite, I can cause the previous URL to call the script barcode.php

See:

https://www.workingwith.me.uk/articles/scripting/mod_rewrite

for more information.

To see mod_rewrite in action, try replacing any of the digits in the following URL with another. Likewise try replacing .gif with .jpg or .png

https://www.somewhereincanada.com/barcode/images/001600064300.gif

Now Google will see links of this format as actual images on my site and index them.

To make it all work, the following was required:

In an .htaccess file in our /barcode/ directory is the following text:


RewriteEngine On
RewriteBase /barcode/images/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /barcode/barcode.php [L]

The previous .htaccess file tells Apache to turn web requests for:

/barcode/images/001600064300.gif

to

/barcode/barcode.php

Note that the folder ‘/barcode/images/’ does not even exist.

https://www.somewhereincanada.com/barcode/images/hjgksad/asd/asd/001600064300.gif

will generate an image also!

On my first attempt, I added the following code to barcode.php. It strips out the ‘001600064300? and ‘gif’ parts from

/barcode/images/001600064300.gif

found in the PHP variable $_SERVER["REQUEST_URI"]. Then took those two values and fed them into the routines that would have normally obtained the values from

?code=001600064300&mode=gif

if ($code == ”)
{
$URIline = $_SERVER["REQUEST_URI"];
//split uri into parts
$urlparts = array();
$urlparts = split(”/”, $URIline);
//get last part image.jpg
$imagename = array_pop($urlparts);
$filenameparts = array();
$filenameparts = split(”.”, $imagename);
$code = $filenameparts[0];
$mode = $filenameparts[1]; //file extension
if ($mode == ”)
{
$mode=’png’;
}
}

If you are comfortable with Regular Expressions, there is a better way to accomplish this.

I could have used mod_rewrite to turn:

https://www.somewhereincanada.com/barcode/images/001600064300.gif

directly into a call to:

https://www.somewhereincanada.com/barcode/barcode.php?code=001600064300&mode=gif

Then I would not have had to modify barcode.php

Here is how I obtained the same results by using Regular Expressions in .htaccess:


RewriteEngine On
RewriteBase /barcode/images/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ([0-9]+).([a-zA-Z]+)$ /barcode/barcode.php?code=$1&mode=$2 [L]

Apache staff say mod_rewrite is difficult to master. So keep it simple.

https://httpd.apache.org/docs/1.3/mod/mod_rewrite.html