D
D
Denis Semenov2017-02-16 07:34:47
Nginx
Denis Semenov, 2017-02-16 07:34:47

How to remove index, .php, .html from url if you have NGINX?

Colleagues, tell me how to painlessly move to nginx and get rid of extra index, .php, .html

server {
    listen      %ip%:%web_ssl_port%;
    server_name %domain_idn%;
    root        %sdocroot%;
    index       index.php index.html index.htm;

    location ~ [^/]\.php(/|$) {
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        if (!-f $document_root$fastcgi_script_name) {
            return  404;
        }

        fastcgi_pass    %backend_lsnr%;
        fastcgi_index   index.php;
        include         /etc/nginx/fastcgi_params;
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Semenov, 2017-02-20
@denisemenov

location / {
    # Удаляем index.html из всех уровней url
    rewrite     ^(/.*)/index.html $1 permanent;
    # Удаляем .php
    rewrite     ^(/.*)\.php(\?.*)?$ $1$2 permanent;
    # Удаляем .html
    rewrite     ^(/.*)\.html(\?.*)?$ $1$2 permanent;
    # Удаляем закрывающий слэш
    rewrite     ^/(.*)/$ $1 permanent;
    # Определяем порядок файлов для обработки запроса
    try_files   $uri/index.php $uri.php $uri/index.html $uri.html $uri/ $uri =404;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question