Mass 301 redirects using .htaccess

I am working on a large project that involves collecting thousands (30,000+) of static web pages and turning them into CMS.

The problem is that many of these pages are duplicates in their directories. I want to keep SEO intact using 301 redirects, however I'm not sure how to do this to make such a big redirect (301).

The following is an example of the current directory structure for pages.

/page.html
/folder/page.html
/folder/subfolder/page.html
/folder/subfolder/anotherfolder/page.html

As you can see, page.html is duplicated in all directories.

For the new CMS, the URL of this page will be /page.html.

+5
source share
2 answers

Working example: http://www.jakeisonline.com/stackoverflow/3345518-mass-301-redirect/page.html

You should be redirected directly to /page.html

Options +FollowSymlinks
RewriteEngine on

RewriteRule ^(.*)page.html /page.html [R=301,NC]

This will always redirect http://www.foo.com/something/something/something/page.html back to http://www.foo.com/page.html using 301 hard redirects.

The rewrite rule does this by looking at the URL, determining if something is before page.html (with the exception of the domain itself), and if it is, will be redirected to 301. Thus, you can literally use any sub-tier, and even as long as it ends on page.html, it is redirected to /page.html in the root directory.

, [R=301,NC],

 R // means simple redirect
 R=301 // means redirect with a 301 header
 NC // means no-case, or case insensitive
 L // can also be used to say 'ignore all the other rules after this'
+6

:

RewriteRule ^([^/]+/)+page\.html$ /page.html [L,R=301]

page.html:

RewriteRule ^([^/]+/)+([^/]+)\.html$ /$2.html [L,R=301]
0

All Articles