Home > Enterprise >  Remove a variable part of an image URL using .htaccess
Remove a variable part of an image URL using .htaccess

Time:10-17

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

CodePudding user response:

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.

CodePudding user response:

Physical file is opening

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

and I added htaccess

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

When I write url it's not opening image. Only listing hta directory

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