Magento htaccess Rewrite and Redirect

Magento htaccess Rewrite and Redirect

July 22, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento htaccess Rewrite and Redirect

URL Rewrite and Redirect for Magento: A Comprehensive Guide

Removing index.php from URLs and redirecting non-www traffic to www is a common requirement for Magento stores looking to optimize SEO performance and user experience. Let’s dive into how you can achieve these changes with proper .htaccess configuration.

Removing index.php from Magento URLs

The primary goal here is to eliminate index.php from your product URLs without breaking any links or functionality on your site. Here’s the correct approach:

Step 1: Enable SEO-friendly URL settings in Magento

Log into your Magento admin panel and navigate to Stores > Configuration > General > Design menu.

Under the “HTML Head” section, set the following options:

  • Use HTTP = Yes
  • Use Web Server Rewrites = Yes

Click on “Save Config”, then “Cache Management” in the top-right corner. Flush all cache types to apply these changes.

Step 2: Update your .htaccess file

Open your Magento store’s .htaccess file located in the root directory of your site. Add or update the following directives:

RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ $1 [R=301,NC,L]

This rule matches any request containing index.php and rewrites it to remove the file from the URL. The [R=301] flag tells the browser that this is a permanent redirect.

Redirecting non-www traffic to www

Ensuring all traffic hits your site via the www subdomain is important for SEO consistency and security purposes. Here’s how you can achieve this:

Update your .htaccess file with the following rewrite rules:

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/%1 [R=301,L]

This rule checks if the requested host does not start with “www. ”. If true, it performs a permanent redirect to the www subdomain.

Ensure you replace http://www.%{HTTP_HOST} with your actual domain name for proper redirection.

Redirecting index.html to the homepage

To clean up the URL when accessing the homepage directly via index.html, add this additional rule:

RewriteCond %{THE_REQUEST} ^.*/index\.html
RewriteRule ^(.*)index\.html$ $1 [R=301,NC,L]

This rule matches any request containing index.html and rewrites it to remove the file from the homepage URL.

Conclusion

Implementing these .htaccess rules will effectively remove index.php from your Magento URLs, redirect non-www traffic to www, and clean up the homepage URL. Always test changes in a staging environment first before deploying them live.

Remember that proper SEO configuration is crucial for driving organic traffic and maintaining search engine rankings for your Magento store. If you’re looking to optimize further or need professional assistance with your ecommerce development needs, feel free to explore our services at Laravel Company.

Stay tuned for more tips on Magento performance optimizations!

Catalog Cache Performance Admin

Our network