Redirecting Magento URLs: Removing index.php
Hello! I’m glad you’re setting up your Magento site and working on URL redirects. Let’s dive into how to remove that index.php from your URLs.
Understanding the Issue
Currently, when you manually type in a URL with index.php, like example.com/index.php/admin, your server processes it correctlyâit takes you to the admin panel. However, the browser URL remains as is, which isn’t what we want for clean, SEO-friendly URLs.
The issue lies not in your .htaccess configuration but rather in Magento’s default settings and how it handles rewrites. By default, Magento sets up rewrites that don’t redirect old-style index.php URLs to the new ones without it.
Configuring Magento Rewrites
To fix this, we need to ensure Magento is correctly handling URL rewrites for both frontend and admin routes:
-
Enable rewrites in the admin panel:
- Go to Stores > Configuration > General > Web.
- Set “Use Web Server Rewrites” to Yes.
-
Regenerate URLs:
- Navigate to System > Index Management.
- Click on “Reindex all” for “URL Rewrite Management”.
Updating your .htaccess File
While your current .htaccess setup works, let’s add a condition to redirect index.php requests:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# This line is new and will redirect index.php URLs
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/+index\.php
RewriteRule ^ / [R=301,L]
# ... (keep the rest of your rules)
</IfModule>
This new condition RewriteCond checks if the request starts with an HTTP method (like GET or POST) followed by /index.php. If it matches, a 301 redirect is issued to remove index.php.
Testing Your Setup
After making these changes:
- Clear your browser cache.
- Manually type in a URL like
example.com/index.php/admin. - Observe the address barâthe URL should now automatically redirect to
example.com/admin.
Remember, 301 redirects are permanent and tell search engines that the old URLs have moved permanently to the new ones.
Summary
In conclusion, to remove index.php from your Magento URLs:
- Enable rewrites in the admin panel.
- Regenerate your URL rewrite data.
- Add a simple condition to your
.htaccessfile for redirection.
This approach ensures both clean, SEO-friendly URLs and proper processing of requests. If you need further assistance or want to explore advanced Magento configurations, be sure to check out our blog on Magento Optimization Best Practices for more tips!
Happy coding!