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.
1 2 3 4 5 |
# Redirect all .htm and .html files to their PHP versions # Code Retrieved at pagecrafter.com - https://pagecrafter.com/redirect-one-file-extension-to-another-with-htaccess/ RewriteEngine On RewriteRule ^(.*)\.htm$ /$1.php [R=301,L] RewriteRule ^(.*)\.html$ /$1.php [R=301,L] |
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:
1 2 |
# Don't use this, as it doesn't always work. RedirectMatch 301 example\.com/(.*)\.html$ http://example.com/$1.php |
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:
1 |
http://example.com/exampledotcom/index.php |
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.
4 Comments on “Redirect One File Extension to Another with .htaccess”
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”
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.
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.
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!