Magento SEO Optimization: Forcing Trailing Slash & Lower Case URLs via htaccess
As an e-commerce developer, you’re always looking for ways to optimize your Magento store’s performance and visibility. Proper URL structure is crucial for both user experience and search engine optimization (SEO). In this post, we’ll guide you through forcing trailing slashes on category pages and ensuring all URLs are lower case using the htaccess file.
The Current Rewrite Section
First, let’s understand your current rewrite section in the .htaccess file:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
# ... existing rules ...
RewriteCond %{REQUEST_URI} !^/(media|skin|js)/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]
</IfModule>
These are the default Magento rules for handling rewrites and routing requests to index.php.
Forcing Lower Case URLs via RewriteMap
To force all URLs to be lower case, you can use a RewriteMap with int:tolower. Add this snippet right after the opening <IfModule> tag:
# Force lower case URLs using RewriteMap
RewriteMap lc int:tolower
# Check for uppercase letters in the request URI
RewriteCond %{REQUEST_URI} [A-Z]
# If found, redirect to lowercase version with 301 status
RewriteRule (.*) ${lc:$1} [R=301,L]
The int:tolower function converts any uppercase letters in the URL to lower case. The RewriteCond checks if the request URI contains any uppercase characters, and the RewriteRule redirects these requests to their lowercase equivalent with a 301 status (permanent redirect).
Forcing Trailing Slash on Category Pages
Next, let’s force trailing slashes on category pages while excluding them from product pages:
# Force trailing slash on non-product pages
RewriteCond %{REQUEST_URI} !^/(.*)\.(html|php)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !index.php$
RewriteRule ^(.*)$ $1/ [L,R=301]
# Remove trailing slash from product, category and search pages
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/
RewriteRule ^([^/]+)$ %1 [L,R=301]
The first set of RewriteCond statements ensures that the rule only applies to non-product pages (excluding HTML/PHP files) and excludes the index.php file. The RewriteRule then appends a trailing slash to all matching URLs.
The second set removes any trailing slashes from product, category, and search pages while leaving them intact for other page types like CMS content.
Conclusion
By following these steps, you’ll have successfully optimized your Magento store’s URL structure:
- Ensure all URLs are lower case using the
RewriteMapmethod withint:tolower. - Force trailing slashes on category pages (excluding product and certain system files).
- Remove trailing slashes from product, category, and search pages.
These changes improve both user experience by maintaining consistent URL formats and boost SEO performance through better crawlability and reduced duplicate content issues identified by search engines like Google.
Remember to thoroughly test these changes in a development environment before deploying them on your live production site.
For more advanced Magento htaccess optimizations and best practices, check out our comprehensive guide at LaravelCompanyâs Magento SEO Optimization.