French Property - Free Credit Report - Human body - Wordpress Themes - Debt Consolidation

PDA

View Full Version : .htaccess rewriting all .php files to .html


blueuniverse
Dec 5th 2004, 12:23 am
For my site, I want to have all the urls rewritten from .php to .html so all the urls in the directory should be done like that automatically. Doing this just for one file is relatively easy (as shown by the code I've got below) but is there anyway just to rewrite all files with the .php extension to .html files?


Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^index\.html$ index.php [T=application/x-httpd-php,L]


If there isn't an automatic solution to do all the urls in the directory, would I just have to keep adding


RewriteRule ^page-name\.html$ page-name.php [T=application/x-httpd-php,L]

below the last line for every different page name?

Thanks

Old Welsh Guy
Dec 5th 2004, 1:33 am
Why are you changing them? why not just have the html pages parsed as php? that way you get the best of both worlds.
You can add this to the htaccess.

AddHandler application/x-httpd-php .php .html .htm

if not then mod rewrite

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

I think that is about it, although I am sure others more capable than I will confirm/deny/improve on the above ;)

blueuniverse
Dec 5th 2004, 12:55 pm
Ok, I'll give that a go. Basically the reason for it was I had to make the pages all php so I could gzip them (by adding the code in the header) but then all my previous pages that were linked to had the .htm extension, so I have to have them as the html pages.

exam
Dec 5th 2004, 7:50 pm
Just curious, what's the code you're using to gzip the output before sending it to the browser?

blueuniverse
Dec 5th 2004, 10:58 pm
I've written something about it in my blog. Gzipping your pages (http://www.pointless-existence.com/archives/2004/12/gzipping-your-pages/)

exam
Dec 6th 2004, 11:00 am
Looks good and pretty easy to add to the php! BTW, I tried to leave a comment on that page, but it didn't work- no error msg, just returned me to the blog w/o posting the comment.

blueuniverse
Dec 6th 2004, 11:10 am
Yeah, it is because I have moderation set up in order to stop the spamming from various robots. It has been moderated now :)

exam
Dec 6th 2004, 1:56 pm
Ok, sorry for submitting the comment twice. :o

nevetS
Dec 6th 2004, 11:35 pm
or you can use mod_gzip in apache. Haven't looked at the documentation in a while, but as I remember it, it negotiates gzip compression for all pages if the browser supports it.

blueuniverse
Dec 7th 2004, 9:09 am
Yeah, that is probably the best solution, but for those on hosting where you have snobby admins, this works best.


EDIT -

Why are you changing them? why not just have the html pages parsed as php? that way you get the best of both worlds.
You can add this to the htaccess.

AddHandler application/x-httpd-php .php .html .htm

if not then mod rewrite

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

I think that is about it, although I am sure others more capable than I will confirm/deny/improve on the above ;) Ahhhh, lol. I finally understand what you mean by that. The top code works perfectly and the .html pages seem to parse as php perfectly. Thanks a lot, that was much easier than the other solution.

Davidf25sc
Sep 26th 2006, 2:37 pm
Lest say i dont have a .htaccess file in the folder i want to make it work as php with html, how do i create a new one, and what lines should i write into it?

adrianctn
Sep 28th 2006, 11:55 am
Lest say i dont have a .htaccess file in the folder i want to make it work as php with html, how do i create a new one, and what lines should i write into it?

Hello David.
You should create htaccess.txt on your pc, write the code below in it, than upload it to your ftp folder and change it's name there( for example, if you're using total commander as a ftp client, you can change the filename just like in windows)

Options +FollowSymLinks

RewriteEngine On

RewriteRule ^([^.]+).html$ /$1.php [QSA,L]


That's it. Regards,

Adrian

brokensoft
Oct 11th 2006, 1:08 am
what's the code you're using to gzip the output ?

salik
Jan 21st 2007, 11:41 pm
<? RewriteRule ^page-name\.html $ page-name.php [T=application/x-httpd-php,L]?>


where i place this code?

salik
Jan 21st 2007, 11:42 pm
how i can change .php files to .html ??plz tell me the simple and easier way ??

Old Welsh Guy
Jan 22nd 2007, 7:42 am
What are you asking here? The answer is above. But if you mean change the extension then you can do that manually on a page by page basis.

bioss
Apr 8th 2008, 7:38 am
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)\.html$ $1.php [L]


this work for you

Yankee85
Apr 21st 2008, 6:52 am
thanks bioss!

chinmay
May 21st 2009, 5:20 am
hi all this is my htaccess code

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^kusal.com [NC]
RewriteRule ^(.*)$ http://www.kusal.com/$1 [L,R=301]
RewriteRule ^(.*)\.html$ $1.php [L]

is there any problem
my nonwww to www is working
but from php to html extension is not working
how can i resolve it any suggestions

jnugroho73
May 25th 2009, 8:37 pm
hi all this is my htaccess code

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^kusal.com [NC]
RewriteRule ^(.*)$ http://www.kusal.com/$1 [L,R=301]
RewriteRule ^(.*)\.html$ $1.php [L]

is there any problem
my nonwww to www is working
but from php to html extension is not working
how can i resolve it any suggestions

Try to use the following code:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^kusal.com [NC]
RewriteRule ^(.*)$ http://www.kusal.com/$1 [L,R=301]

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