Website Designers and Webmasters

Dedicated to all the tasks Webmasters, Website Developers and Website Designers find themselves facing.

Redirecting Page Extensions with htaccess

If you are not familiar with htaccess, you are missing out. It is one of the greatest things about linux/php hosting. One little file that gives you a wide range of control. Here is one of the most useful parts of htaccess:

Redirecting Many Pages From .html to .php (or “/”) at Once

Today, I am finishing converting a static, HTML website to WordPress. So all 58 pages that used to be /page.html are now /page/ and it would be a pain to go through all of the .html pages and force redirects to their new page. I found this page on Web Weaver that explains this well:

Changed file extension?

This example is perfect if you’ve decided to switch to .php from .html pages. It will look for any .html page and redirect it to .php (ie http://www.example.com/yourpage.html and redirect it to http://www.example.com/yourpage.php). Now, be careful with this, it does mean any html page. I did this on one of my sites and had totally forgotten I had an iframe with .html content on some pages… I didn’t notice for weeks that it was broken :S.
So learn from my mistake 😉 check, double check, then check again.

RedirectMatch 301 (.*)\.html$ http://www.example.com$1.php

In my case, I put most of the pages into a subfolder (and without .php) so my htaccess redirect looks like this (example website for placeholder):

RedirectMatch 301 (.*)\.html$ http://www.websitesinaflash.com/website-services$1/

In this example, the $1 get’s replaced with everything from the (.*) which is everything after the domain name and before the .html.

Enjoy,
Ashton Sanders

1 Comment »

  1. I have a question. I would like to redirect the other way.

    Background: I had a googlepages site that i got rid of when they migrated to google sites, i opted tohave them redirect traffic to a new site I created. within googlepages each page was shown without an extension
    “mysite.googlpages.com/page”. Now search results are showing the new page without the “.htm”
    To ensure that users can reach my pages I want to redirect from “/page” to “/page.htm”

    Thanks

    Comment by Eli — November 10, 2009 @ 2:02 am

RSS feed for comments on this post. TrackBack URL

Leave a comment