Answer the question
In order to leave comments, you need to log in
How to redirect a part of a URL in Nginx?
Good day
I ask for help, because. couldn't figure it out on my own.
After registering the domain and creating the site in Yandex.Webmaster, I saw that the old site left a lot of "already broken" links like:
site.ru/userfiles/images/zammojxhnlu.jpg,
site.ru/userfiles/images/img_8035.jpg, site .ru/storage/repertuar_images/cfbe2f9f6a8ca8d6b... etc. etc.
All these links have common parts of the URL like:
site.ru/userfiles/
site.ru/storage/
site.ru/repertuar/
site.ru/abonement/
site.ru/media/
site.ru/photogallery/
site.ru/ festsimf2/
Actually, the question is: what kind of regular expression should be written so that all links where these parts of the URL occur (userfiles, storage, repertuar folders, etc.) go 301 redirects to site.ru.
The code below makes a redirect if the URL ends with {folder name}:
location ~* /(repertuar|userfiles|storage|abonement|media|photogallery)(/?).* {
rewrite ^ / permanent;
}
Answer the question
In order to leave comments, you need to log in
Do not use regular expressions in the server
section , they kill the logic of finding the location you need
location /repertuar {
rewrite ^/repertuar(.+) $1 last;
}
location /userfiles{
rewrite ^/userfiles(.+) $1 last;
}
location /storage{
rewrite ^/storage(.+) $1 last;
}
In general, rewrite is also not recommended in nginx
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question