skip to Main Content

I have problem with creating .htaccess rule for creating url for small resized images.

I have saved images inside upload/images/ directory and it looks like this:

somedomain.com/upload/images/bigimage-some_name123.jpg

I have already created url via PHP so i can call resized image:

somedomain.com/upload/images/small/bigimage-some_name123-150x100.jpg

From this small url I need 4 parameters:

  • name: bigimage-some_name123
  • extension: .jpg or jpg
  • width: 150
  • height: 100

Via .htaccess need to create url so I can fetch all parameters above and show resized image on the fly:

somedomain.com/image-resizer/index.php?image=upload/images/bigimage-some_name123.jpg&width=150&height=100

Colleague of mine send me this .htaccess line and I need to rewrite it so it can work on my project.

RewriteRule
^upload/images/small/([^/.]+)-([0-9]{3,5})([^/.]+)-([0-9]{2,3})x([0-9]{2,3}).jpg$
module/show_image.php?name=../files/$2$3.jpg&width=$4&height=$5 [L]

2

Answers


  1. You should not try to extract information from the path in the .htaccess rewrite rule. It’s much easier doing that in PHP. So what I do is to feed the whole SEO optimized URL to the PHP page behind the rewrite rule. My .htaccess looks like this:

    RewriteRule ^(.*)$ /page.php?SEOPath=$1 [NC,L,QSA]
    

    You can see how simple the rewrite rule now is. Then in PHP I work on the SEO path, and extract the wanted information, which is not too difficult.

    Of course you still need to adapt this to your situation. For instance, restrict it to .jpg files only. In your case it could be:

    RewriteRule ^(.*).jpg$ /module/your.php?SEOPath=$1 [L]
    
    Login or Signup to reply.
  2. This can be well handled in rewrite rules and it is much faster than doing in PHP also. You can use this rule in root .htaccess:

    RewriteEngine On
    RewriteBase /
    
    RewriteRule ^(upload/images)/small/(.+?)-(d+)x(d+).(jpe?g)$ image-resizer/index.php?image=$1/$2.$5&width=$3&height=$4 [L,QSA,NC]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search