S
S
Stepan2016-04-21 09:37:43
Nginx
Stepan, 2016-04-21 09:37:43

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;
}

Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Daniil Muidinov, 2016-04-28
@steff

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

B
bamond, 2016-04-28
@bamond

location ~* ^/(repertuar|userfiles|storage|abonment|media|photogallery) {
rewrite ^ http://www.site.ru$request_uri? permanent;
}
should be something like this.. didn't check

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question