Need to create a nginx 301 redirect.
From: /blog/some-article
To: /blog/article/some-article
Current nginx configuration:
location /blog/(.*)$ {
rewrite ^/blog/(.*)$ ^/blog/article$1 redirect;
}
Current configuration result:
This URL with multiple 301 redirects
/blog/article/article/article/article/article/article/article/article/article/article/article/article/article/article/article/article/article/article/some-article
How to configure this redirect properly?
Any help will be appreciated, thanks.
CodePudding user response:
You can use a negative lookahead assertion in the regular expression to avoid the redirection loop.
This needs to be done at the location
level.
You can avoid using two regular expressions by using a return
statement instead or the rewrite
. rewrite...redirect
is implemented as return 302
, whereas a rewrite...permanent
would be implemented as return 301
.
For example:
location ~ ^(/blog/)((?!article/). )$ {
return 301 $1article/$2;
}