V
V
VirgiL2014-09-02 15:44:29
linux
VirgiL, 2014-09-02 15:44:29

How to make a regular expression for a redirect in nginx?

Good day to all.
There is such a task,
it is necessary to redirect a url of the form: site.ru/page1/ru/content/ to a page of the form: newsite.com/page1/ru/#content
the following part of the nginx config successfully copes with this:
location /page1/ {
rewrite ^( .*)/(.*)/$ http://newsite.com$1/#$2 permanent;
}
but, the task also arose to process pages of the form: site.ru/page1/ru/content (that is, without "/" at the end) and redirect them to the page: newsite.com/page1/ru/#content.
The question is, can this be done with a single regular expression? If possible, please help with writing it, because I have already tried many options. If this is not possible, then how to describe these redirects within the same nginx config.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lynn "Coffee Man", 2014-09-02
@VirgiL

Very simple.

location /page1/ {
    rewrite ^(.+?/)(.*)/$ http://newsite.com$1#$2 permanent;
    rewrite ^(.+?/)(.*)$  http://newsite.com$1#$2 permanent;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question