Magento 2 Remove Index.php from URLs (Redirect URLs with Index.php to URLs without it)
1
up 3 down 0
starvotestarvotestarvotestarvotestarvote
Thank you for voting !

Magento 2 Remove Index.php from URLs (Redirect URLs with Index.php to URLs without it)


In this post, I will guide you on how to remove index.php from Magento URLs. Many times magento developers notice the issue of URLs being accessed with index.php. Optimizing the URL length always makes your website SEO friendly and google recommends clearly to redirect all versions of URLs to the preferred one.

For example you can access your magento store home page with https://www.yourdomain.com/index.php/ and https://www.yourdomain.com/. Google always encourages the URL structure to be as simple as possible and redirects all live version of URLs to one preferred version. We can get this easily fixed using some redirects code in htaccess file.

Problem with the below htaccess code


RewriteEngine on
RewriteCond %{REQUEST_URI} !/admin/
RewriteRule ^index\.php/(.+)$ /$1 [R,L]
RewriteRule ^index\.php/?$ / [R,L]

When you will start searching for Magento 2 index.php issue, many times you will come across the above solution. Many developers recommend the above code as correct to fix URLs redirect without index.php. But wait, did you tested the above code personally in your htaccess file ??

The above code works fine and it redirects the URLs from index.php to URLs without it but this code conflicts with other available redirects on the htaccess file. For example your website has one backlink from third party server coming to your domain https://yourdomain.com/product.html and for some reason you changed the URL from https://yourdomain.com/product.html to https://yourdomain.com/product2.html. Now to avoid lose in traffic and take advantage of that backlink, you may need to set a redirect on your htaccess file for this old URL https://yourdomain.com/product.html. This is because you can’t control third party server. Simply you will set a redirect to new path to avoid traffic lose.

Now here comes the problem, the above solution for index.php problem will set your all old redirects to root domain. Any other redirects available on htaccess file will be overwritten. To fix this problem, use the below code which is tested and working great. The below code redirects all URLs with index.php to URls without it and keep safe all available redirects on the file.


RewriteEngine on
RewriteCond %{THE_REQUEST} ^.*/index.php     
RewriteRule ^(.*)index.php$ https://domain.com/$1 [R=301,L]

Just make sure to replace https://domain.com with your domain name. Also make sure, you have SSL enabled and working. I would highly recommend not to use the website without SSL.

1 thought on “Magento 2 Remove Index.php from URLs (Redirect URLs with Index.php to URLs without it)”

Something To Say ?

Your email address will not be published. Required fields are marked *

*