Its very easy and common to get htaccess code to 301 redirect the duplicate home page URLs that may exist for your website home page. We looked at how to achieve that in an earlier post titled non-www to www. Googles Matt Cutts also has a good article here about that issue.
However trawling through the net or fiddling about with htaccess codes to resolve the duplicate inner page URLs you may have is a much different matter. WordPress and many other CMS’s handle this easily without any intervention required from the user.
If however you have a static HTML site and/or a PHP based site then this may not be so easy. You may have fixed your duplicate home page URL’s which is good for your SEO but you will still have inner page duplicate URL’s like so:
- www.example.com/innerpage/
- www.example.com/innerpage/index.php
- www.example.com/innerpage/index.html
To htaccess 301 redirect duplicate inner page URLs you have to be careful. Inadvertently adding the wrong code to your htaccess may give you a 404 page not found error for that page or worse for the whole of your site.
Luckily, its fairly simple to add code to the htaccess file. To htaccess 301 redirect duplicate inner page URLs you simply need to pop this code into your htaccess file. We would recommend adding this below the non-www to www code as per our earlier post:
1 2 | RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*index\.(php|html?)[#?]? RewriteRule ^(([^/]+/)*)index\.(php|html?)$ http://example.com/$1 [R=301,L] |
The first line of that code gives the parameters that need to be looked for. So in this case we are looking for index.html and index.php. You could of-course replicate this for inner-pages that are called innerpage.html and/or innerpage.php or any other variation that you could think of. To keep it simple we have used the index example.
Now all you would need to do is pop that into your htaccess file with the code referenced in the earlier non-www to www post and hey presto all you duplicate inner page URL’s will disappear. This will now leave you with one simple url that looks like:
www.example.com/innerpage/
Notice the / the URLs will be appended with that and you will no longer have the index.html/php referenced. this is good for static HTML and PHP based websites.
Home Page ONLY 301 redirect to a NEW domain
Those of you who want to redirect just your homepage to a new page may find that its not as easy to do with wordpress. If you want to redirect your WordPress home page to a new domain BUT leave all your other pages in tact OR redirect those to new destinations then the code that you will need to use is:
1 | RedirectMatch 301 ^/$ https://example.com/ |
should work nice and easy. If you are having issues with this working then move it below ALL your other redirects and it should fix any issues you may be having 🙂