Removing index.php from Magento URLs
When you install Magento, your URL structure includes index.php. For example, your homepage might look like this: https://www.mydomain.com/index.php/.
While this is perfectly functional, removing the index.php part can make your site’s URLs cleaner and more professional-looking. Here’s a step-by-step guide on how to achieve this in Magento 2.
Understanding the issue
By default, Magento uses the index.php file as the entry point for every URL request. This is necessary because of how Magento’s architecture works with its front controller pattern.
However, you can configure Magento to handle requests without needing to include index.php. This requires a few changes to your server configuration and core files.
Prerequisites
Before proceeding, ensure you have:
- A backed-up version of your site (always a good idea before making major changes)
- Access to the Apache/Nginx server configuration file for your Magento instance
- Basic knowledge of editing server configuration files
Step 1: Rename index.php
The first step is to rename the index.php file. This won’t remove it from the URL, but it’s a necessary preliminary step.
Rename the index.php file in your Magento root directory to something like old-index.php. Make sure not to delete this file; you’ll need it if anything goes wrong and you want to revert the changes.
Step 2: Change the Server configuration
Next, you need to configure your web server to process URLs without including index.php.
For Apache:
Open your primary Apache config file (usually found in /etc/apache2/) and add the following lines inside the <VirtualHost> section of your Magento site:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
For Nginx:
Add or edit the server block for your domain in the main Nginx config file (usually /etc/nginx/sites-available/your_domain):
server {
listen 80;
server_name www.yourdomain.com;
location / {
index index.php index.html;
try_files $uri @rewriteapp;
}
location @rewriteapp {
rewrite ^/(.*)$ /index.php?/$1 permanent;
}
}
After making these changes, save the file and restart your server to apply them.
Step 3: Update Magento’s base URL
Finally, you need to update the base_url setting in your database configuration:
UPDATE core_config_data
SET value = 'http://www.yourdomain.com/'
WHERE path = 'web/unsecure/base_url';
And for secure URLs:
UPDATE core_config_data
SET value = 'https://www.yourdomain.com/'
WHERE path = 'web/secure/base_url';
Step 4: Clear the cache
After updating the base_url, clear Magento’s cache using the following command in your terminal:
php bin/magento cache:clean
Conclusion
Removing index.php from your Magento URLs can improve your site’s aesthetic and make it easier for users to navigate. However, it requires careful attention during the server configuration process.
Make sure you backup your site before making these changes and test thoroughly after applying them. If anything goes wrong, simply revert the changes (putting old-index.php back as index.php).
Remember that Magento is a complex system with many configurations. It’s always wise to consult official documentation or experienced developers if you’re unsure about any specific setting.
For more detailed guides and best practices on Magento development, check out Laravel Company’s Magento resources.