Removing index.php from Magento URL While Using Nginx

Removing index.php from Magento URL While Using Nginx

August 1, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Removing index.php from Magento URL While Using Nginx

Removing index.php from Magento URL While Using Nginx

If you’re running an e-commerce store on Magento and want to clean up your URLs by removing index.php, you’ve come to the right place. I’ll guide you through the process of doing just that while using Nginx as your web server.

Understanding the Current Setup

Your current setup is a common one for many Magento developers:

  • Operating System: Ubuntu 12.04 LTS
  • Web Server: Nginx 1.4.1
  • E-commerce Platform: Magento 1.7.0.2

This combination provides a stable and secure environment for running your online store.

The Goal: Removing index.php from URLs

You want to get rid of index.php in your URL structure without affecting the functionality or performance of your site. This is a common practice that enhances the user experience by presenting cleaner, more professional-looking URLs.

Analyzing Your Current Nginx Configuration

Let’s examine your existing vhost configuration:

server {
  root     /path/to/root/domain/html;
  index    index.php;
  error_log /path/to/error/log/error.log;
  access_log /path/to/transfer/log/transfer.log;
  server_name   servername.com;

  location / {
    index index.html index.php;
    try_files $uri $uri/ @handler;
    expires 30d;
  }

  # More directives...
}

Identifying the Problem

The issue lies in how Nginx is configured to handle PHP files and the Magento application.

Notice these lines:

location ~* .php$ {
  if (!-e $request_filename) { rewrite / /index.php last; }
  expires off;
  fastcgi_pass 127.0.0.1:9000;
  # More directives...
}

location @handler { rewrite / /index.php; }

These rules tell Nginx to pass all PHP requests to index.php, effectively inserting it into your URLs.

The Solution: Redirecting Requests

To fix this, we need to modify the way Nginx handles PHP files and Magento’s internal routing logic. We’ll achieve this by updating our vhost configuration as follows:

server {
  root     /path/to/root/domain/html;
  index    index.php;
  error_log /path/to/error/log/error.log;
  access_log /path/to/transfer/log/transfer.log;
  server_name   servername.com;

  location / {
    try_files $uri $uri/ @handler;
    expires 30d;
  }

  # More directives...

  location ~* .php$ {
    if (!-e $request_filename) { return 404; }
    expires off;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
  }

  location @handler {
    rewrite ^/ /index.php?/$request_uri last;
  }
}

What We Changed

The key modifications are:

  1. In the location ~* .php$ block, we changed the internal redirect ("rewrite ...") to a 404 error ("return 404;"). This tells Nginx not to process URLs ending in .php.

  2. In the location @handler block, we updated the regex pattern to /index.php?/$request_uri last;. This instructs Nginx to pass the entire URL path as an argument to PHP.

Implementing the Solution

To apply these changes:

  1. Open your vhost configuration file in a text editor.

  2. Replace the existing content with the updated version provided above, ensuring you adjust the paths and domain names accordingly.

  3. Save the file and close it.

  4. Restart Nginx to apply the changes: sudo service nginx restart

Verifying the Results

After following these steps, if everything is configured correctly, you should no longer see index.php appearing in your Magento URLs:

https://www.example.com/some-category

Instead of:

https://www.example.com/index.php/some-category

Conclusion

Removing index.php from Magento URLs when using Nginx involves a few strategic modifications to the vhost configuration. By updating the PHP and handler location blocks, we can clean up our URLs without compromising the functionality or performance of our e-commerce store.

Remember that making changes to your server configuration requires careful attention to detail. Always test thoroughly in a staging environment before applying updates to your live site.

For more advanced Magento configurations and best practices, I recommend exploring the resources available at Laravel Company. They provide comprehensive guides tailored for developers like you.

Magento 1 Indexing Database Performance

Our network