Redirect One File Extension to Another with .htaccess

Redirect File Extension To Another Htaccess

For something seemingly so simple, it’s amazingly hard to find a good solution that redirects all files with a given extension on your site to another. I spent a long time Googling and testing and still kept running into issues.

But my wasted time is your gain! Because I’ve figured it out.

As it turns out, there is more way than one to do it, but some of the more popular solutions won’t work in many configurations which is quite frustrating.

To save you all time, I’m just going to share the solution right here. In this example, we’re redirecting both .htm and .html files to their counterparts ending in .php. This code should work as-is without any additional modification, which is awesome. Just drop it into the beginning of the .htaccess file in your site’s root folder, or create it if it doesn’t exist.

This sets up a permanent redirect for every path with .htm or .html in it, and sends visitors to the same file but ending in .php.

For example, a visitor to would be redirected to .

Line #4 redirects .htm to .php, and line #5 redirects .html to .php. You can replace those extensions with whichever you’d like to redirect, or remove one of the lines if you only need one of them.

Now, you may have seen other sources that claim this is the best way to set up this type of redirect:

And you know what? Sometimes it works! If you host in an environment where your website has its own hosting instance, completely isolated from the other sites: it should work.

But if you have multiple sites in one hosting account, it is unlikely to. This includes most of those hosting at any of the common hosts such as Godaddy, BlueHost, HostGator, or any of the others.

The problem is, the page you get redirected to will include the folder name for the local site. So if you wanted to visit this page: , you would likely be redirected to something like this:

Which is clearly not the same page. The “exampledotcom” is the folder name for that site. It is the root folder where the files are held, and because of the nature of this type of hosting environment, this .htaccess code returns that folder name.

I don’t see any clear benefit to using that method even if it works, so my recommendation is to use the first solution.

One final type: use the .htaccess tester to verify things should be working. You may have a server issue or some other misconfiguration with the rest of your .htaccess file if it works at the tester but not on your site.

I hope this saves someone some frustration! Good luck.

About Brian Johnson

Brian Johnson is a website developer and designer living in Minneapolis, Minnesota with a passion for code and WordPress. He spends his days building WordPress websites for small businesses, developing new code with the online community, and living life.

2 Comments on “Redirect One File Extension to Another with .htaccess”

  1. Thanks for your detailed guide. I can follow and redirect a file easily. Can you also recommend a plugin helping with this? I’m afraid of making changes to my .php files.

    1. I recommend the “Redirection” plugin. In theory you could set up the same regex values in that plugin and achieve the same thing. Maybe I’ll add something about that to this post later!

Leave a Reply

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