htaccess, PHP, SEO

Redirecting Page Extensions with htaccess

1 Comment

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.

So learn from my mistake. Check, double check, then check again.

RedirectMatch 301 (.*)\.html$ https://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$ https://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

  • 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

Leave a Reply to Eli Cancel reply

Your email address will not be published. Required fields are marked *