@springimport

Как лучше сделать редирект для старых url в nginx?

Перед тем как поменять сервер на Nginx на сервере стоял Apache с такой настройкой для старого домена.
По запросу example.com/specials был редирект на example-new.com/special-wholesale-deals.html.

<VirtualHost *:80 *:443>
    ServerName www.example.com
    ServerAlias example.com
    DocumentRoot /var/www/example.com/site

    ### turn on rewrite engine
    RewriteEngine on

    ### define the map
    RewriteMap urlmap txt:/var/www/example.com/apache-products-redirects.txt

    ### require a match in the map to continue
    RewriteCond ${urlmap:/$1} ^.+

    # Choose one of the following rewrite rules:

    ### rewrite the entire request and return the value from the map file
    #RewriteRule ^/(.+)$ ${urlmap:/$1} [R,L]
    ### rewrite the entire request to a new domain
    RewriteRule ^/(.+)$ https://www.example-new.com/${urlmap:/$1} [R=permanent,L]

    Redirect permanent / https://www.example-new.com/
</VirtualHost>


Для Nginx разработал такую конфигурацию

map $request_uri $result_uri {
    default "";

    "/specials" "special-wholesale-deals.html";

    #include /var/www/example.com.com/nginx-products-redirects.txt;
}

server {
    listen 80;
    server_name example.com www.example.com;

    # store url
    set $target_store_url https://www.example-new.com/;

    # All files in it
    location / {
        rewrite ^ $target_store_url$result_uri? permanent;
    }
}


Работает редирект если запросить /specials, но если /specials/, то уже нет, вернее на главную, но хотелось бы чтобы слэш не влиял.

Мне кажется, что нужно очистить $request_uri. Это можно сделать с помощью регулярки, наверное. Или можно добавить регулярку в map.

Подскажите как сделать.
  • Вопрос задан
  • 205 просмотров
Решения вопроса 1
@BorisKorobkov
Web developer
Или можно добавить регулярку в map.

1. Добавить новую строку с "/specials/"
2. Или сделать регулярку "~/specials/?"

3. Но в данном случае правильнее без map, а
location ^~ /specials {
    return 301 /special-wholesale-deals.html;
}

или
location ^~ /specials {
    rewrite ^ /special-wholesale-deals.html permanent;
}
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы