.htaccess rewrite url - show html file as jpg

2019-06-14 12:52发布

问题:

I have image.html and I want It to be shown in browser as image.jpg, because there are some operations when showing picture, that can't be done before or after. Is it even possible with .htaccess and url rewriting?

I tried

RewriteEngine On
RewriteBase /    
RewriteRule ^image\.html$ image.jpg

but without any luck.

Thanks for all answers

回答1:

First of all, you'll need let's say a PHP script. Call it image.php. After that, use a GET parameter to handle the image name. Let's call it name. To call a picture you need image.php?name=allo.

Now. In the PHP script, you need to specify in the header it's an image, JPEG. Required. After that, just "print" the picture within the page.

All you need after is Rewrite rule.

RewriteEngine On
RewriteBase /    
RewriteRule ^image\.php?name=(.*)$ /images_jpeg/$1

The header is really important. This is why it doesn't work with an HTML file.