6

I want to convert .php extension to .html extension using .htaccess rules

Is this something I can do, and if so, how?

Josh
  • 10,961
  • 11
  • 65
  • 108
redcoder
  • 119
  • 1
  • 1
  • 5
  • I cleaned up your question a bit to make it more clear, I hope you don't mind. – Josh May 20 '10 at 23:24
  • The logical way would be the other way round: requests of `foo.html` would be rewritten to `foo.php`. – Gumbo May 21 '10 at 11:31

2 Answers2

12
RewriteEngine on
RewriteRule ^(.*)\.html$ $1.php [nc]

Uses mod_rewrite. $1 indicates first part of the regex and [nc] means not case sensitive.

You can take a look at this article: http://roshanbh.com.np/2008/02/hide-php-url-rewriting-htaccess.html

EDIT: Removed a line. You do not need that I think. Also, commented on what I did.

  • it doesn't seems to work.. i have a directory of tradesalvage/demo under localhost .. wat should i put? – redcoder May 20 '10 at 23:53
  • 1
    I think you do not need that first line so I edited that. Make sure that mod_rewrite in your Apache http.conf is loaded and not commented out. I tried it on my machine. It works. Here is what I did: 1.) added those two lines in my .htaccess file. Saved in the htdocs/webroot. Created a index.php file. and went to browser and accessed it as index.html. Let me know. –  May 21 '10 at 00:53
  • 1
    I did this exact thing yesterday and it worked. The only problem is mod_rewrite was not enabled before. Google for "verify mod_rewrite is working" if in doubt – satyajit May 21 '10 at 01:01
  • ooh sorry, my initial thought was wrong. I thought i can access throuhg index.php and the URL will display index.html. If this is the case, i might want to just display index without any extension. I tried with the following but still nothing happen, the URL display as usual as index.php RewriteEngine on RewriteRule ^(.*)\$ $1.php [nc] – redcoder May 21 '10 at 06:38
  • I think this article might help you: http://eisabainyo.net/weblog/2007/08/19/removing-file-extension-via-htaccess/ or this: http://www.blog.highub.com/apache/http-server/remove-file-extension-using-htaccess/ –  May 21 '10 at 13:42
  • make sure you do 301 for .php to .html of that specific file. –  Jul 07 '14 at 16:23
3

Maybe try

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

RewriteRule ^(.*)\.html$  $1.php [NC,L]
Don
  • 542
  • 3
  • 6