A
A
Alexander Vladimirovich2015-02-15 12:16:34
Nginx
Alexander Vladimirovich, 2015-02-15 12:16:34

How to write regular expressions for nginx rewrite?

Hello. Help write 2 regular expressions.
1. If the query string contains a dot in the last part (ignoring query parameters), then there should not be a trailing slash. For example:

/news/index.html
/top.news/item/123.htm
/test/test/te.st/test/any_text.any_text

2. If the query string does NOT contain a dot in the last part (ignoring query parameters), then there must be a trailing slash. For example:
/
/news/
/top.news/item/
/test/test/te.st/test/
/test/te.st/te.st/test/

Everywhere 301 redirect
for the first option, I got the following,
rewrite ^(.*\.[^/]*)/$ $1 permanent;
and the second does not work yet

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Rodyushkin, 2015-02-16
@polyanin

The second, offhand, can be done like this:
rewrite ^(.*/)?([^\.]*[^/])$ $1$2/ permanent;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question