Redirect One Webpage Extension to Another

May 21, 2013 .htaccess, Development

I needed to redirect all webpages that had the extension of .html to the equivalent page with the extension of .php. For example, apple.html to apple.php, orange.html to orange.php and banana.html to banana.php.

However, not every page had an equivalent page created. There was no banana.php so we needed to keep banana.html as if there weren’t any rules in place.


RewriteCond %{REQUEST_FILENAME} (.*).html$
RewriteCond %1.php -f
RewriteRule ^(.*).html$ $1.php [R,L]

Stack Overflow user szemian posted a great solution that only takes three lines in the .htaccess file. The first checks to see if the requested webpage has a .html extension, the second checks if the .php page exists and the third line completes the redirect if it does.

Let's Talk About Your New Website

View Portfolio Contact Today