9

Greetings!

I'm trying to replace .php extensions with .html

So far I got:

RewriteRule ^(.*)\.html $1.php

... it works nicely when url like /site/page.html is entered (and page.html does not physically exist but page.php does).

However what I'd like to have is when /site/page.php is entered the viewer sees only /site/page.html in the browser location.

Is that doable or do I have to set up explicit redirects for each page? :-(

Thanks in advance.

ps: dev environment I'm using is XAMPP on os x if it makes any difference

vector
  • 7,334
  • 8
  • 52
  • 80
  • 3
    If you are going that far why even have an extension at all? just have /site/page reference the .php page. It looks cleaner. – RDL Dec 28 '10 at 19:45
  • You can check out this tutorial on how to replace `.php` with `.html` using htaccess https://helponnet.com/2021/04/27/how-to-convert-php-extension-into-html-using-rewriterule-in-htaccess/ – Amit Verma Aug 02 '21 at 13:30

4 Answers4

20
RewriteRule ^(.*)\.php $1.html [R=301,L]
RewriteRule ^(.*)\.html $1.php

edit after pulling white rabbit out of the hat

RewriteEngine on  
RewriteBase /

RewriteCond %{THE_REQUEST} (.*)\.php  
RewriteRule ^(.*)\.php $1.html [R=301,L]  

RewriteCond %{THE_REQUEST} (.*)\.html  
RewriteRule ^(.*)\.html $1.php [L]  
  • ... something's wrong, when I enter "http://localhost/site/index.php" I get "http://localhost/Applications/XAMPP/xamppfiles/htdocs/site/index.html", not sure what to make of it – vector Dec 28 '10 at 20:00
  • this puts my Apache into endless loop – German Rumm Dec 28 '10 at 20:09
  • ... rewrite base didn't help :-( – vector Dec 28 '10 at 20:14
  • 1
    ... that did it! Except for the peculiarity of dealing with XAMPP, for rewrie base I have to use '/site/' instead of just the '/' That's what happens when I don't work with the stuff on regular basis :-) Thanks – vector Dec 29 '10 at 15:18
  • just nee to remove this line *RewriteBase /* – Fury Jan 12 '14 at 15:31
  • 1
    after apply this solution .php pages are working fine as .html but existing .html page are stopped working.. could you please help for what to do for existing .html files? – Vijaysinh Parmar Jul 18 '18 at 03:06
0
RewriteEngine on  
RewriteBase /

RewriteCond %{THE_REQUEST} (.*)\.php  
RewriteRule ^(.*)\.php $1.html [R=301,L]  

RewriteCond %{THE_REQUEST} (.*)\.html  
RewriteRule ^(.*)\.html $1.php [L]
Code Lღver
  • 15,573
  • 16
  • 56
  • 75
  • 1
    after apply this solution .php pages are working fine as .html but existing .html page are stopped working.. could you please help for what to do for existing .html files? – Vijaysinh Parmar Jul 18 '18 at 03:00
-1

Give this a try:

RewriteEngine on
RewriteBase /
RewriteRule ^(.*)\.html$ $1.php [L] 
THE DOCTOR
  • 4,399
  • 10
  • 43
  • 64
-3

I think that this is much simple

AddType application/x-httpd-php .html

it just process every .html file as normal .php

if you want it only for current dir

<Files *.html>
ForceType application/x-httpd-php
</Files>
Jan Šafránek
  • 893
  • 10
  • 13