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 https://pagecrafter.com/example.htm would be redirected to https://pagecrafter.com/example.php .

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: http://example.com/index.html , 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.

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

  1. Thanks: what does this do in your script? [R=301,L]
    I am “renameing/redirecting” all pdf files to pdf?v0 as a kludge to bypass a cache that no browser and no common .htaccess
    edit seems to be recognized by Google, Opera, or Edge
    So your one line per change is the cleanest yet, although I’m doubtful about how the question mark may be seen by .htaccess

    FileETag None

    Header unset ETag
    Header set Cache-Control “max-age=0, no-cache, no-store, must-revalidate”
    Header set Pragma “no-cache”
    Header set Expires “Wed, 08 Jan 1972 05:00:00 GMT”

    1. The 301 part makes it a 301 redirect, which means it is intended to be permanent. The “L” means this is the last rule, and no further rules in the .htaccess file should be taken. The new URL established at this point is the final one.

      Unfortunately, I don’t think this method will work if you are including question marks. You’ll need to use mod-rewrite instead of the “Redirect” directive. That is beyond the scope of this article.

  2. 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 *