F
F
FreeAero2013-11-22 17:20:49
Nginx
FreeAero, 2013-11-22 17:20:49

How to write rewrite rules for nginx that will add a slash at the end of the link?

Hello, I am turning to you after Google, help me create rewrite rules for nginx, which will add a slash at the end of the link as follows
site.ru/controller => site.ru/controller/
site.ru/controller?params=2 => site. ru/controller/?params=2
And at the same time do not rewrite files with the extension jpg png css jss..etc
I know that in htaccess it would be like this

RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} !\.
RewriteRule ^(.*) %{REQUEST_URI}/ [R=301,L]

winginx.ru/htaccess - Didn't help.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
G
globalmac, 2016-03-13
@globalmac

I do something like this:

location / {
    rewrite ^([^.\?]*[^/])$ $1/ permanent;
    try_files $uri @clean_url;
}

Force Trailing Slash:
This line adds a slash / at the end of every URL, only if there is no period or parameters in the URL. That is, after example.com/index.php or example.com/do?some=123, the slash will not be placed.

V
vapmaster, 2013-11-22
@vapmaster

# Исключение по расширению файлов
RewriteCond %{REQUEST_URI} !(jpg|png|gif|ico)
# Дальше всё по старому
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} !\.
RewriteRule ^(.*) %{REQUEST_URI}/ [R=301,L]

I
Ivan, 2013-11-22
@iSage

http://stackoverflow.com/questions/645853/add-slash-to-the-end-of-every-url-need-rewrite-rule-for-nginx#answer-3912675

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question