Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

htaccess : redirect an image path to a PHP script

I'd like the following image URL

http://www.example.com/image-provider/article/1275449_inline3_scale_700xauto.jpg

to be redirected to the following PHP script that will actually generate the content

http://www.example.com/image-provider?url=article/1275449_inline3_scale_700xauto.jpg

I've tried the following syntax in my .htaccess

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/image-provider/(.*)$ image-provider?url=$1 [L]

With no success so far:I still get a 404. Is there something wrong in my .htaccess ?

like image 279
benoit Avatar asked Aug 09 '26 01:08

benoit


1 Answers

You can use this rule:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(image-provider)/(.+)$ $1?url=$2 [L,QSA,NC]

Couple of issues in your rule:

  1. Leading slash is not matched in .htaccess
  2. You must use .+ to avoid matching /image-provider/ as an URI.
like image 60
anubhava Avatar answered Aug 10 '26 15:08

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!