Redirect all .html .shtml pages


I’ve read through umpteen articles on this but can’t get anything working properly.

I’ve redesigned a site for a friend. All the pages in his old site are either .html pages or .shtml pages.

I just want to globally redirect any pages with either of those extensions (i.e. definitely from the old version of the site) to the new homepage using .htaccess. Everything I’ve tried either gets 404s, breaks the site or causes a loop.

Hopefully fairly straightforward even though it’s beaten me!

Many thanks in advance

You mean something like this?

Option +SymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \\.s?html$ http://%{HTTP_HOST}/ [L,R=301]

That redirects any request that either ends in .shtml or .html and is not a real file (so if you have a contact.html that does is exist the rule won’t be fired) to the website root.

Although a better way would be to map all old URLs to new ones using Redirect (if there aren’t a whole lot, like hundreds, of pages) or mod_rewrite (if there are a whole lot of pages)

The advantage of this kind of redirect is that google will give the PR from the previous URL to the new URL. If you redirect everything to / I don’t think it will. Plus it’s not really the nicest thing to do for visitors who come looking for a specific page but are redirected to the home page instead. Whenever that happens to me I always leave instantly.

Thanks Scallio.

Really appreciate your response. I agree that, as a solution, it is a bit blunt. However, the current site is cluttered and dated and they want a bit of a cull and to start producing fresh content.

I will have a look and see if there are pages which reproduce old content and set up some appropriate page redirects.

Is it possible to use your example as a catch-all but only if more specific redirects have not been found.

i.e. oldsite/contact.html redirects to newsite/contact
but if not found *.html redirect to the home page?

yes just put the Redirect before my RewriteRule. Something like

Redirect permanent /contact.html /contact.php

Option +SymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \\.s?html$ http://%{HTTP_HOST}/ [L,R=301]


(the .php is just an example, you can use the URLs however you like, including extension-less if you use the proper code).

Thank you so much. That is really a great help.