Home > Mobile >  Rewrite rule htaccess to ignore everything after last slash
Rewrite rule htaccess to ignore everything after last slash

Time:11-16

I always have a single file per folder named test.* (* = jpg, png, video file etc) which is stored in different sizes as following

https://www.example.com/images/1500/01/100/test.jpg

https://www.example.com/images/1500/01/500/test.jpg

https://www.example.com/images/1500/01/900/test.jpg

https://www.example.com/images/1500/01/1800/test.jpg

I would like to create a rule in htaccess which will allow me to load the different files using a link.

As an example for the first image I would like to be able to load the file using the following links:

https://www.example.com/images/1500/01/100

https://www.example.com/images/1500/01/100/

https://www.example.com/images/1500/01/100/whatever

https://www.example.com/images/1500/01/100/somethingelse

The logic would be to always load the file which will always be named test however without calling it test in the link and in addition be allowed to write anything behind the trailing slash.

Is it possible using plain htaccess. I was trying with the following however it does not give the result I am looking for:

RewriteEngine On
RewriteRule ^images/(.*)/([^/] ) images/$1/$2/test.jpg

CodePudding user response:

If you have a hierarchy of 3 folders deep, and they are all numeric you can do something like this:

RewriteEngine On
RewriteRule ^images/([0-9] )/([0-9] )/([0-9] ). ?$ images/$1/$2/$3/test.jpg [L]

This will match

https://www.example.com/images/1500/01/100

https://www.example.com/images/1500/01/100/

https://www.example.com/images/1500/01/100/whatever

https://www.example.com/images/1500/01/100/somethingelse


If they are not always numeric:

RewriteEngine On
RewriteRule ^images/([^/] )/([^/] )/([^/] ). ?$ images/$1/$2/$3/test.jpg [L]
  • Related