mod_rewrite change extensions .gif/.jpg to .png - .htaccess

The rule I have been fruitlessly working with doesn't work, I am trying to get all images in 1 directory to load as .png The images in the directory are a mixture of .png, .jpg and .gif
I want to be able to load the file tree.jpg by going to tree.png (no files have the same name). I am sure my mistake is obvious or my entire attempt is wrong, I just can't work it out.
The htaccess file is in the same folder with the images, which is called /thumbs
RewriteEngine On
RewriteRule ^([^.]*)\.gif$ /thumbs/$1.png [R,L,NC]
I tried this also, but it just givens a broken link to both .gif and .png versions
RewriteEngine on
RewriteRule ^(.+)\.gif$ $1.png
I tried this too, but it adds in my server path to the URL for some reason
RewriteEngine on
RewriteRule ^([^.]*)\.gif$ $1.png [R,L,NC]

You can use this rule:
RewriteEngine on
RewriteRule ^([^.]+)\.png$ /$1.gif [L,NC,R]

Related

How to serve webp from a different folder using .htaccess

I have a folder /assets/admin/images/products which contains .jpg and .png files and another folder /assets/admin/images/products/webp with the webp version of .jpg and .png files with name filename.webp (jpg and png is not added in file names)
How do i serve webp version of image from different folder using htaccess
I found some information at https://github.com/vincentorback/WebP-images-with-htaccess but it only serves from same folder.
I tried below htaccess code, but it didn't work
RewriteCond %{HTTP_ACCEPT} image/webp
RewriteCond %{DOCUMENT_ROOT}/webp/$1.webp -f
RewriteRule (.+)\.(jpe?g|png|gif)$ %{DOCUMENT_ROOT}/webp/$1.webp [T=image/webp,E=REQUEST_image]
Your code is good but you don't need %{DOCUMENT_ROOT} on the last line with RewriteRule. This is the problem.
RewriteCond %{HTTP_ACCEPT} image/webp
RewriteCond %{DOCUMENT_ROOT}/webp/$1.webp -f
RewriteRule (.+)\.(jpe?g|png|gif)$ webp/$1.webp [T=image/webp,E=REQUEST_image]

Use .htaccess to load files from different folders

I have many files in directory /full/
that's why I would like to spread files to /full1/, /full2/, /full3/ folders on server but to save original URL like
http://my-domain.com/full/article-with-text
to determine which files are where to put I'd like to define it on the URL mask like
^/full/a$ from folder /full1/
^/full/b$ from folder /full2/
tell me please how to build correct .htaccess ?
Try this
RewriteRule ^full/a(.+)?/?$ http://my-domain.com/full1/a$1 [NC,L]
RewriteRule ^full/b(.+)?/?$ http://my-domain.com/full2/b$1 [NC,L]
RewriteRule ^full/c(.+)?/?$ http://my-domain.com/full3/c$1 [NC,L]
...
RewriteRule ^full/z(.+)?/?$ http://my-domain.com/full26/z$1 [NC,L]
e.g,
http://my-domain.com/full/a-one.txt
will mask the url
http://my-domain.com/full1/a-one.txt

Redirect a specific file type from one directory to another

I am trying to redirecting only the jpg and png image files from a specific directory that has other file types that need to remain.
As a test, I tried this and other variations without success to move only the jpg.
RedirectMatch 301 ^/wp-content/uploads/(.*)\.jpg$ http://www.example.com/wp-content/uploads/redirected_media/$1.jpg
How can I redirect only the jpg and png image files using htaccess without affecting the other file types?
You can use that, in your .../uploads/.htaccess :
RewriteEngine on
RewriteBase /wp-content/uploads/
RewriteRule ^([^/]+\.(?:jpe?g|png))$ redirected_media/$1 [R=301,NC,L]
If redirect it's not for the same server:
RewriteEngine on
RewriteRule ^([^/]+\.(?:jpe?g|png))$ http://www.example.com/wp-content/uploads/redirected_media/$1 [R=301,NC,L]

.htaccess URL rewrite only rerwites one page

I'm rerwiting a URL to point to a different URL under the hood. But it seems like all the other files in different directories referenced by index.php (e.g. css files, JS files, etc) do not get redirected. How can I accomplish this?
I have
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/test($|/)
RewriteRule .*$ ../index.php?orgid=4 [L]
I found the answer is
RewriteRule ^(test)(.*)/?$ index.php?orgid=4 [L]
In the browser, this will show as example.com/test, while under the hood the url is actually example.com/index.php?orgid=4

.htaccess rule stacking and 404 redirect

The user requests a PNG image, say: http://server/myfolder/subfolder/1234.png. If that .png doesn't exist, then I want to show instead a .gif in the same folder of the same name except for the .gif extension, which should exist.
Another addition is that we recently changed the directory structure so the URL that I need to check may have changed. For example, the URL above should be able to be reached by requesting either directly as http:// server/myfolder/subfolder/1234.pngor by http://server/oldfolder/1234.png. The change in the directory structure can be expressed as:
RewriteRule oldfolder/(.*) myfolder/subfolder/$1 [L,QSA]
In both directories, I need to check if the file exists, and use a different image instead if necessary. Any help would be greatly appreciated.
Try adding the following to the .htaccess file in the root directory of your site.
It should work with your changed file structure too.
RewriteEngine on
RewriteBase /
#if the file does not exist
RewriteCond %{REQUEST_FILENAME} !-f
#and is in any of these folders: /myfolder/subfolder or /folder1 or /folder2
RewriteCond %{REQUEST_URI} ^/(myfolder/subfolder|folder1|folder2)/ [NC]
#and its a png, then try to serve the gif instead
RewriteRule ^(.+)\.png$ $1.gif [L,NC]

Resources