Mobile app version of vmapp.org
Login or Join
Chiappetta492

: Reuse values in htaccess to make more flexible code In a webshop I've build I want to make the imagename more seofriendly. It's now a uniqid(), in my example the filename will be 1243: I've

@Chiappetta492

Posted in: #Htaccess #Images #Optimize

In a webshop I've build I want to make the imagename more seofriendly.
It's now a uniqid(), in my example the filename will be 1243:

I've got the following working, I want the htaccess to check the following:
if 1234.jpg exists -> redirect to the jpg
if 1234.jpeg exists -> redirect to the jpeg
if 1234.png exists -> redirect to the png
(not in the code, but a all 3 dont exist-> redirect to no-image.jpg would be great)

I have the following code (this is a simplefied version!), but I feel this could be done more efficient:

RewriteCond %{REQUEST_URI} ^/product_images/([0-9]+)
RewriteCond %{DOCUMENT_ROOT}/images/%1.jpeg -f
RewriteRule ^product_images/([0-9]+) images/.jpeg [L]

RewriteCond %{REQUEST_URI} ^/product_images/([0-9]+)
RewriteCond %{DOCUMENT_ROOT}/images/%1.jpg -f
RewriteRule ^product_images/([0-9]+) images/.jpg [L]

RewriteCond %{REQUEST_URI} ^/product_images/([0-9]+)
RewriteCond %{DOCUMENT_ROOT}/images/%1.png -f
RewriteRule ^product_images/([0-9]+) images/.png [L]



All 1st lines are identical and will remain that way
All 2nd lines are the same, apart from the .ext
All 3rd lines are the same, apart from the .ext
Every 3rd line has the same .ext as the 2nd line


My .htaccess isn't bad, but I have no clue where to start with this. Anyone who can push me in the right direction? I prefer explanation above the plain answer, so I can fix this myself in the future

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Chiappetta492

1 Comments

Sorted by latest first Latest Oldest Best

 

@Murphy175

As you say, in all three rule sets the first rule is the same. So you might benefit from nested conditions...

Here's an existing answer that should get you started.
stackoverflow.com/questions/14030878/is-it-possible-to-have-nested-rewrite-conditions-in-htaccess
Also you may find it useful when optimizing your rewrites to turn on your rewrite log.
stackoverflow.com/questions/9632852/how-to-debug-apache-mod-rewrite
I think there's a danger of over-optimizing here though. You're not likely to get a lot of real world performance improvement out of this. I think code readability and maintainability would be the priority for something like this.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme