Signup/Sign In
PUBLISHED ON: MARCH 10, 2023

Remove index.php from URL in Nginx server

In order to improve the user experience and make URLs more readable, it is often desirable to remove the "index.php" from the URL of a website. This can be done by making a few changes to the nginx server configuration.

Why should you remove index.php to clean URLs

The configuration changes outlined above enable URL rewriting on the server, and specify a set of rewrite rules that will remove the "index.php" from the URL. These changes also ensure that the server is able to properly process PHP files, which is necessary for most web applications.

Implementing these changes can provide a number of benefits, including improved search engine optimization and a more user-friendly URL structure. By removing the "index.php" from the URL, website visitors will be able to easily see which pages they are accessing, and search engines will be able to more accurately index and rank the content on your website.

Remove index.php from URLs

First, we need to ensure that the server is able to interpret URL rewriting rules. This is done by adding the following line to the server block:

rewrite_log on;

Next, we need to add a set of rewrite rules that will remove the "index.php" from the URL. This can be done by adding the following lines to the server block:

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

Finally, we need to ensure that the server is able to properly process PHP files, by adding the following lines to the server block:

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

Customize Nginx server configuration

  1. Open the nginx configuration file using the command sudo nano /etc/nginx/nginx.conf.

  2. The final configuration should look similar to this:
  3. server {
        listen 80;
        server_name domain.com;
    
        rewrite_log on;
    
        location / {
            try_files $uri $uri/ /index.php?$args;
        }
    
        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
        }
    }
  4. Once these changes have been made, save the configuration file and restart the nginx server by running the following command:
    $ sudo service nginx restart

After the server has been restarted, we should be able to access our website without the "index.php" in the URL. For example, if our website was previously accessible at "http://domain.com/index.php", it should now be accessible at "http://domain.com/".

Conclusion

In conclusion, removing the "index.php" from the URL on an nginx server can be accomplished by making a few changes to the server block in the nginx configuration file. By enabling URL rewriting and adding the appropriate rewrite rules, we can ensure that the "index.php" is not present in the URL of our website. Additionally, by configuring the server to properly process PHP files, we can ensure that our website continues to function as expected.



About the author:
Pradeep has expertise in Linux, Go, Nginx, Apache, CyberSecurity, AppSec and various other technical areas. He has contributed to numerous publications and websites, providing his readers with insightful and informative content.