shlomicohen
shlomicohen

Reputation: 101

htaccess change part of REQUEST_URI name if IMAGE not found 404/403

hi i need help with htaccess and change part of REQUEST_URI

i have a images on alow of folders like this url:

https://pic.domain.com/images/111/34%20(Big).jpg

only if that image does not exsist on server 404 or 403, i need to change the url to

https://pic.domain.com/images/111/34.jpg

i need to remove from the %20(Big) string and redirect to new url

i have tryied this:

RewriteEngine on
RewriteCond %{REQUEST_URI} !-f
RewriteRule ^([^/]+)/%20(Big)/?(.*)$ /$1/$2 [R=301,L]

but i dont know what i need to do next.

thanks!

Upvotes: 0

Views: 101

Answers (1)

Mohammed Elhag
Mohammed Elhag

Reputation: 4302

You could do so many scenarios according to your own requirements , one is like this :

RewriteEngine On
RewriteCond %{REQUEST_URI} !-f
RewriteRule ^([^\s]+)\s([^\s]+)\.(.+)$ /$1.$3 [R=301,L]

Note: clear browser cache then test it .

If you want to match wrong request with only .jpg extension do this :

RewriteEngine On
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} \.jpg$
RewriteRule ^([^\s]+)\s([^\s]+)\.(.+)$ /$1.$3 [R=301,L]

If both jpg or jpeg do this :

RewriteEngine On
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} \.jpe?g$
RewriteRule ^([^\s]+)\s([^\s]+)\.(.+)$ /$1.$3 [R=301,L]

Upvotes: 1

Related Questions