R
R
rsenmakh2019-03-19 14:32:16
Nginx
rsenmakh, 2019-03-19 14:32:16

Nginx: how to remove index.php from URL?

Dear colleagues, please tell me how to fix this config in order to get rid of index.php in the URL
If possible, then without if-s

server {
        listen 80 default_server;
        listen [::]:80 default_server;
        server_name www.domainname.com domainname.com;
        
        return 301 https://domainname.com$request_uri;
}

server {
        listen 443 ssl default_server;
        listen [::]:443 ssl default_server;
        server_name www.domainname.com;

        ssl_certificate /etc/letsencrypt/live/domainname.com/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/domainname.com/privkey.pem; # managed by Certbot

        return 301 https://domainname.com$request_uri;   
}

server {
        listen 443 ssl http2;
        listen [::]:443 ssl http2;
        server_name domainname.com;

        ssl_certificate /etc/letsencrypt/live/domainname.com/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/domainname.com/privkey.pem; # managed by Certbot
        include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

        root /var/www/domainname.com/www/public_html;
        index index.php index.html;
        
        charset UTF-8;
        autoindex off;
        error_page 403 =404;

        location / {
               try_files $uri $uri/ /index.php?$args;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.2-fpm-domainname.com.sock;
        }

    	location = /favicon.ico {
    		log_not_found off;
    		access_log off;
    	}

    	location = /robots.txt {
    		allow all;
    		log_not_found off;
    		access_log off;
    	}

        location ~ /\.ht {
                deny all;
        }

        location ~* \.(jpg|jpeg|gif|png|ico|pdf|ppt|bmp|rtf|svg|otf|woff|woff2)$ {
                expires max;
        }

        location ~* \.(js|css|txt)$ {
    		expires 3d;
        }

        gzip on;
        gzip_vary on;
        gzip_proxied any;
        gzip_comp_level 6;
        gzip_buffers 16 8k;
        gzip_disable "msie6";
        gzip_http_version 1.1;
        gzip_types text/plain text/css text/xml application/json application/xml application/x-javascript application/javascript text/javascript text/json;
}

Now the site is available at the following URLs:
https://domainname.com
https://domainname.com/index.php
The most common solution I found on the web
server {

 ....

  rewrite ^/index.php/(.*) /$1  permanent;

 ....

}

But in my case it doesn't help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey, 2019-03-19
@VELIK505

if ($request_uri ~ ^/index.php) {
rewrite ^.*$ / permanent;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question