N
N
Nurba Nurba2018-05-17 09:12:14
htaccess
Nurba Nurba, 2018-05-17 09:12:14

How to rewrite image url .htaccess?

Good morning dear hackers.
Never worked with .htaccess file
I want to convert links to images .
here are some links for example

https://oldsite.ru/mugs/Flagler/2018/2018-00001170.jpg
https://oldsite.ru/thumbs/Flagler/2018/2018-00001170.jpg

for such
https://newsite.ru/Eachtime/Flagler/mugshots/2018/2018-00001170.jpg
https://newsite.ru/Eachtime/Flagler/thumbnails/2018/2018-00001170.jpg

this directory may not be /2018/ or it may be like 2017
/Flagler/ this part may change depending on the categories
It seems that there is an opinion that it can be done through regular expressions. But I wrote regular expressions in php for example functions preg_match .
And how is this done in htaccess do not tell me?
And if you don’t make regexp difficult, I would also like to see the main structure in order to finish it later

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Viktor Taran, 2018-05-17
@nurba91

Actually this is a pregmatch

RewriteCond %{HTTP_HOST} ^oldsite\.ru$
RewriteRule ^mugs/(.+[^/])/([0-9]{4,}/2018\-00001170\.jpg)$ https://newsite.ru/Eachtime/$1/mugshots/$2? [L,R=301]

Let's go in order.
If this is such and such a domain (it can be omitted because it will be registered only on this site, so it can be deleted)
(.+[^/])/ any number of characters from 1 to infinity In addition / to /, in general, select everything up to the next slash.
Well, we combine all this happiness into the first group ()
. We do the same with the date
[0-9]{4,}from zero to nine 4 times, and already combine a fairly large part of the url with the second group.
Actually, we substitute the selected groups in the places of the new url $1 and $2
the Last action, go to the toaster, click solved;)
ps
It would be more literate to make the first group through greedy and lazy quantifiers, but as practice shows, people do not catch up with what it is at all, in general it will work like that;)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question