Onder Akbulut
Onder Akbulut

Reputation: 50

Remove a variable part of an image URL using .htaccess

I have an image that is accessible with the following URL:

https://localhost/hta/-7524-34-B.webp

I need to be able to access the same image with a URL like the following and remove the <something> part:

https://localhost/hta/<something>-7524-34-B.webp

How can I do this with .htaccess?

UPDATE:

I added the following .htaccess:

RewriteEngine On
RewriteRule ^.*?(?=&). /hta/$1 [NC,L]

When I write the following URL it's not opening the image. Only listing the hta directory.

http://localhost/hta/something-else&-7524-34-B.webp

Upvotes: 1

Views: 60

Answers (1)

MrWhite
MrWhite

Reputation: 45923

To internally rewrite a URL of the form /hta/<something>-7524-34-B.webp to /hta/-7524-34-B.webp (removing <something>) you could do something like the following near the top of your root .htaccess file:

RewriteEngine On
RewriteRule ^hta/[^/]+(-7524-34-B\.webp)$ hta/$1 [L]

The regex [^/]+ matches <something>, which is discarded. The remaining part of the URL-path -7524-34-B\.webp is captured and this is referenced in the substitution string (2nd argument) with the $1 backreference.

Upvotes: 1

Related Questions