Question

How to insert custom location code in NGINX (trying to remove .php .html extensions)

Hi I’m a total newbie at editing Nginx file configs, would be awesome if someone could help me out by either pointing out where I have to insert the code in my below config or insert it for me and post the full edited config code!

I’m trying to rename urls for example https://domain.tld/contact.php (and .html) to https://domain.tld/contact

CODE I NEED TO INSERT

location / {
    try_files $uri $uri.html $uri/ @extensionless-php;
    index index.html index.htm index.php;
}

location ~ \.php$ {
    try_files $uri =404;
}

location @extensionless-php {
    rewrite ^(.*)$ $1.php last;
}

My http nginx config file

# Redirect www and non-www http to non-www https
server {
    listen IP:80;
    server_name www.domain.com domain.com;
    return 301 https://domain.com$request_uri;
}
server {
    listen      IP:80;
    server_name domain.com www.domain.com;
    root        /home/admin/web/domain.com/public_html;
    index       index.php index.html index.htm;
    access_log  /var/log/nginx/domains/domain.com.log combined;
    access_log  /var/log/nginx/domains/domain.com.bytes bytes;
    error_log   /var/log/nginx/domains/domain.com.error.log error;

    location / {

        location ~* ^.+\.(jpeg|jpg|png|gif|bmp|ico|svg|css|js)$ {
            expires     max;
        }

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

            fastcgi_pass    127.0.0.1:9002;
            fastcgi_index   index.php;
            include         /etc/nginx/fastcgi_params;
        }
    }

    error_page  403 /error/404.html;
    error_page  404 /error/404.html;
    error_page  500 502 503 504 /error/50x.html;

    location /error/ {
        alias   /home/user/web/domain.com/document_errors/;
    }

    location ~* "/\.(htaccess|htpasswd)$" {
        deny    all;
        return  404;
    }

    location /vstats/ {
        alias   /home/user/web/domain.com/stats/;
        include /home/user/conf/web/domain.com.auth*;
    }

    include     /etc/nginx/conf.d/phpmyadmin.inc*;
    include     /etc/nginx/conf.d/phppgadmin.inc*;
    include     /etc/nginx/conf.d/webmail.inc*;

    include     /home/user/conf/web/nginx.domain.com.conf*;
}

Little question, since I am using VESTACP which creates two config files (nginx.conf for http and snginx.conf for https) which one do I edit? I’m redirecting everything to https non www.


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

@dominik99

This answer can help you, however you need to edit the HTTPS config file as everything is rdirected to HTTPS.

Hope this helps.