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

2
  • 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. Commented Dec 28, 2010 at 19:45
  • You can check out this tutorial on how to replace .php with .html using htaccess helponnet.com/2021/04/27/… Commented Aug 2, 2021 at 13:30

4 Answers 4

21
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]  
Sign up to request clarification or add additional context in comments.

6 Comments

... something's wrong, when I enter "localhost/site/index.php" I get "localhost/Applications/XAMPP/xamppfiles/htdocs/site/index.html", not sure what to make of it
this puts my Apache into endless loop
... rewrite base didn't help :-(
... 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
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?
|
0
RewriteEngine on  
RewriteBase /

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

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

1 Comment

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?
-1

Give this a try:

RewriteEngine on
RewriteBase /
RewriteRule ^(.*)\.html$ $1.php [L] 

Comments

-2

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.