24

I want to update all the pages on a website to use include for the footer and header. So I have to change a lot of .html pages to .php.

So i'm looking for a way to redirect all pages that end with .html to the same url but ending in .php.

Max
  • 1,175
  • 3
  • 12
  • 22

7 Answers7

50
RewriteEngine On
RewriteRule ^(.*)\.html$ $1.php [L]

If you want it to be done as a redirect instead of just a rewrite modify the [L] to [L,R]

James C
  • 14,047
  • 1
  • 34
  • 43
  • 1
    I went for RewriteRule ^(.*)\.html$ $1.php [R] as with the as the L flag seems to have problems with some of my other Rules. Thanks for the help! – Max May 13 '11 at 10:31
  • 4
    the L flag prevents is a 'stop' flag. any rule following it will not be executed http://httpd.apache.org/docs/2.2/rewrite/flags.html – roberthuttinger Nov 05 '12 at 19:47
  • 1
    This isn't working for me under Windows 8.1 and XAMPP 1.8.3. I ended up using: "RewriteRule ^(.*)\.html$ /$1.php [L]". Note that the only addition is a / before the $1.php part. Cheers. – Mario Awad Jul 23 '15 at 09:23
8

You could do a more simple approach and have all your html files be processed as php files by adding the following line to your .htaccess

AddHandler application/x-httpd-php .php .html
Jordonias
  • 5,778
  • 2
  • 21
  • 32
  • 3
    Original approach, but you should mention this solution adds some overhead, as it forces PHP to parse ANY request (even static ones) which is pretty much pointess and could slow down your performace a bit. – Defrag Sep 27 '17 at 14:34
6

mod_rewrite to the rescue!

RewriteEngine On
RewriteRule ^(.+)\.html$ $1.php
Pål Brattberg
  • 4,568
  • 29
  • 40
2

If you want an actual HTTP 301 Moved Permanently Redirect

RewriteEngine on
RedirectMatch 301 ^(.*)\.html$ $1.php

or

RewriteEngine on
RewriteCond %{THE_REQUEST} \ /(.+)\.php
RewriteRule ^ /%1.html [L,R=301]
Dušan Maďar
  • 9,269
  • 5
  • 49
  • 64
  • 1
    You have mistakenly interchanged .html and .php, as the question is "redirect all .html extensions to .php". a little correction and its working fine. Correction: RewriteEngine on RewriteCond %{THE_REQUEST} \ /(.+)\.html RewriteRule ^ /%1.php [L,R=301] – ajaykc86 Apr 30 '22 at 15:54
1

In your apache httpd.conf file you can add

AddType application/x-httpd-php .html

to make .html files go through the php parser before they are served to the user. You can also add this directive to your .htaccess file. The second method may not work depending on how your host is setup.

wewals
  • 1,447
  • 9
  • 9
  • Because PHP can be run many different ways, the exact syntax of that line, and the necessity of other lines to go along with it, changes a lot. I've had Linux admins struggle to find the correct syntax and combination of addtype / addhandler lines to make it work. (Just realized you were suggesting an edit to httpd.conf my experience is with .htaccess. I don't know if the results are the same.) – TecBrat Oct 15 '19 at 14:16
0

Note that the AddType command will process your existing html file as php. If what you wanted was to replace an existing html file with a new php file you need to use the rewrite rule.

0

The rule need needs an / in front of $1 otherwise, at least in my cPanel, the file I'm redirected to has the full path, including the home folder, etc.

RewriteEngine On
RewriteRule ^(.*).html$ /$1.php [L,R]
Leon Kunštek
  • 555
  • 1
  • 7
  • 21
OliverP
  • 13
  • 2