I had recently had an issue with a website where two different URL’s pointed to the exact same page.The website serves up dynamic content, and generates products pages on the fly, which is the norm for e-commerce sites. But instead of using a query string, we use the mod rewrite to change the url into a prettier form (mod rewrite). The issue is that when I inherited the site, the previous developer had used the url in this form.
while the current version removes the store directory.
These both point to the exact same content, and Google has indexed both versions, which leaves me with a duplicate content issue. As duplicate content is not good for Search Engine Optimization, I had to figure out a way to do a 301 redirect for any page that has /store/ within their url. And for those who are not aware, a 301 redirect tells the search engines that the old page has permanently moved to a new location.
So the solution was to use a .htaccess directive called ‘RedirectMatch‘, which is a smarter way to redirect urls. I just needed to add a single line of code to my .htaccess file:
And it was as simple as that.
1 - the function you want to execute, and in this case it’s redirectMatch
2 - the HTTP 1.1 status code, which is telling the web server to do a 301 redirect
3 - the regular expression pattern that needs to be matched. My regular expression is telling the server to redirect any url that has the /store/ string within it.
4 - This is the location to redirect, but I want to just copy the url right after /store/. so I place a $1 and $2 as a place holder for the pattern within the parenthesis. This way /store/page_name.html will become just /page_name.html. See the relationship in the diagram below.
So I successfully performed this redirect with redirectMatch, and the search engine will no longer index urls with /store/ inside the url. This same technique can be used to redirect one domain name to another, which is important to preserve your page rank if you ever decide to switch domain names. If you have any questions on this, feel free to fill out a comment.
Add this to your blog:
(Copy & paste code)