Problem:
You have a Magento website with Base URL = http://example.com/
Working: http://example.com/category-one.html
Not working: http://www.example.com/category-one.html (redirecting to homepage)
The only difference in the above two URLs is that the second URL has “www” in it.
Similary, you may have another Magento website with Base URL = http://www.example.com/
In this case,
Working: http://www.example.com/category-one.html
Not working: http://example.com/category-one.html (redirecting to homepage)
So, it depends upon the Base URL (System -> Configuration -> Web). If your base URL has ‘www’ in it then all URLs with ‘www’ will work and URLs without ‘www’ will redirect to homepage.
Solution:
By adding few lines of code in your .htaccess file, you can 301 redirect the undesired/unused URL to desired/used URL. The 301 status code means that a page has permanently moved to a new location.
1) Redirecting ‘non-www’ URLs to ‘www’ URLs
Suppose, you have base URL as http://www.example.com/ then you can do 301 redirect to all ‘non-www’ URLs to ‘www’ URLs.
So, http://example.com/category-one.html will be redirected to http://www.example.com/category-one.html
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
2) Redirecting ‘www’ URLs to ‘non-www’ URLs
You can also redirect ‘www’ URLs to ‘non-www’ URLs.
So, http://www.example.com/category-one.html will be redirected to http://example.com/category-one.html
RewriteCond %{HTTP_HOST} !^example\.com
RewriteRule (.*) http://example.com/$1 [R=301,L]
This should solve the problem.
Hope it helps. Thanks.