Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mass 301 redirect using .htaccess

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

The issue is many of these pages are duplicates within their directories. I want to keep the SEO intact by using 301 redirects, however, I am not sure how to go about doing such a large redirect (301).

Here is an example of the current directory structure for the 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 the directories.

For the new CMS the URL to that page would just be /page.html.

like image 537
michaelcarrano Avatar asked Jul 27 '10 15:07

michaelcarrano


People also ask

How do I redirect my site using a .htaccess file?

Use a 301 redirect . htaccess to point an entire site to a different URL on a permanent basis. This is the most common type of redirect and is useful in most situations. In this example, we are redirecting to the "example.com" domain.

What is a 301 .htaccess redirect?

A 301 Permanent Redirect permanently redirects one URL to another. You set up a 301 redirect using . htaccess to send visitors to a new URL and tell search engines that a page has moved so that the new page can be properly indexed.

Should I enable 301 .htaccess redirect?

The . Because the WordPress 301 redirect is not always reliable, we recommend issuing the 301 redirect via your . htaccess file. Another benefit is that the . htaccess redirect is slightly faster than redirecting via PHP, because it is loaded even before the rest of the page.


1 Answers

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

You should be redirected straight 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 a 301 hard redirect.

The rewrite rule does this by looking at the URL, determining if anything before page.html is included (excluding the domain itself) and if it is, will 301 redirect. So you can literally use any sub-level, and as long as it ends with page.html, it will redirect to /page.html in the root directory.

In case you're wondering what [R=301,NC] means,

 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'
like image 105
jakeisonline Avatar answered Sep 21 '22 23:09

jakeisonline