How can I remove index.php from the address while redirect requests to index.php via fastcgi?

How can I remove index.php from the address while redirect requests to index.php via fastcgi?

August 23, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How can I remove index.php from the address while redirect requests to index.php via fastcgi?

Cleaning Up URLs: Removing index.php via NGINX Rewrite Rules

You’ve set up a basic Magento e-commerce site on an Ubuntu server using PHP-CGI via NGINX, but you’re currently seeing the full index.php path in your URL structures. This can be cleaned up and made more user-friendly through careful configuration of the NGINX rewrite settings.

The Current Setup

Your current configuration looks good for handling standard PHP pages:

# catch rewritten and standard URLs
location ~* ^(/index.php)?(.*) {
    include /etc/nginx/fastcgi_params;
    fastcgi_pass  127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root/index.php$1;
    fastcgi_read_timeout 600;
}

This block catches all requests, including those with the full index.php path, and passes them to PHP-FPM on port 9000 for processing.

Removing index.php from URLs

To achieve the desired clean URL structure without the index.php component, we’ll modify this block to use NGINX’s built-in rewrite functionality. This involves catching all requests that do not contain index.php, and then forwarding them to a rewritten path that includes it.

Here’s how you can accomplish this:

# catch standard URLs (without index.php)
location / {
    try_files $uri $uri/ /index.php?$query_string;
}

# catch rewritten and standard URLs with index.php
location ~* ^(/index.php)?(.*) {
    include /etc/nginx/fastcgi_params;
    fastcgi_pass  127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$uri;
    fastcgi_read_timeout 600;
}

In this updated configuration:

  • The new location / block catches all requests without an index.php component. It attempts to serve the requested URI or directory, falling back to passing the request to index.php if it fails.

  • The original block remains for handling URLs that include index.php.

How This Works

The magic is in the first location block:

try_files $uri $uri/ /index.php?$query_string;

This directive tells NGINX to attempt to serve either the exact path ($uri) or the path with a trailing slash ($uri/). If these fail (which they will for most dynamic content), it falls back to routing the request to index.php, passing along any query string parameters.

Conclusion

With this setup, all your URLs should now appear clean and polished without the explicit index.php component:

http://domain.com/apparel/shoes.html

Remember to restart your NGINX service after making these changes for them to take effect:

sudo systemctl restart nginx

This approach provides a simple yet effective way to maintain the flexibility of handling dynamic content via PHP while presenting a clean, static-looking URL structure to the end user. It’s a best practice in modern web development that contributes to both SEO and user experience.

For more in-depth guides on optimizing Magento performance with NGINX configurations, be sure to check out our detailed tutorials at LaravelCompany.com.

Indexing Database Configuration

Our network