Change image extension from htacess

we have site with 1000 image and 60 pages (static site), we are trying to optimizing website, we convert all image to webp, now we need update image extension like jpg, jpeg, png, gif to webp all over page section which seems challenging doing manually. how can we replace image extension redirect all image extension to webp so that we dont need update every image extension manually going every page, every section.

how can rewrite all image extension to webp from htacess

1 Like

Are you saying you have converted your images without changing the HTML code? So your website is essentially broken?

1 Like

A quick search suggests that you would do something like

RewriteRule ^(.*).jpg$ http://example.com/$1.webp [R=301,L]

and repeat the line for each different filetype that you want to redirect.

(I found that here: https://htaccessbook.com/add-remove-change-file-extensions-htaccess/ )

1 Like

yes, thats what we mean, all over site there are image type like jpg, png, jpeg and so on, we want change image type to webp in html

that worked but how can i do it for other type like png, jpeg with same line

doesnt work for background image in css

I think I covered that here:

Why would you need it to be on the same line? I don’t know if that’s possible. The article I linked to (I know nothing about .htaccess, I just searched for “htaccess rewrite file extension”) explains in detail what it’s doing.

1 Like

Instead of redirecting every image, could you use an editor with “search & replace” to edit the HTML?
There is no need to manually edit 1000 image URLs.

yes im aware of that but in cpanel, around 60-70 pages find and replace for every image extension it bit time consuming

So was converting all of your images to webp, but you found time to do that :wink:

Presumably you converted them all to webp to speed up loading of your site. Meanwhile it will take longer for each document to process you .htaccess file. :upside_down_face:

Depending on what editor you are using, the process should be automated to some extent. Such as having a “Replace All” option, and an option for the scope of the search/replace, such as just the current document, or all within a project or folder.

yes im aware of that but in cpanel, around 60-70 pages find and replace for every image extension it bit time consuming

IDE are smart enough to replace among all file at once but in cpanel i dont know the process to replace at once to all, downloading and change through text-editor and reuploaded is only left out options

That suggests that you don’t already have a local and backup copy/copies of all the web site files. Surely you don’t do code changes directly on the live server? That would sound like a bad idea to me.

3 Likes