1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

.htaccess rewriting all .php files to .html

Discussion in 'Apache' started by blueuniverse, Dec 5, 2004.

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

    Code (Text):
    1.  
    2.   Options +FollowSymLinks
    3.   RewriteEngine on
    4.   RewriteBase /
    5.   RewriteRule ^index\.html$ index.php [T=application/x-httpd-php,L]
    6.  
    If there isn't an automatic solution to do all the urls in the directory, would I just have to keep adding

    Code (Text):
    1.  
    2.   RewriteRule ^page-name\.html$ page-name.php [T=application/x-httpd-php,L]
    below the last line for every different page name?

    Thanks
    blueuniverse, Dec 5, 2004 IP
  2. Old Welsh Guy

    Old Welsh Guy Well-Known Member

    Messages:
    2,699
    Likes Received:
    291
    Best Answers:
    0
    Trophy Points:
    155
    #2
    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 ;)
    Old Welsh Guy, Dec 5, 2004 IP
  3. blueuniverse

    blueuniverse Guest

    Messages:
    217
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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.
    blueuniverse, Dec 5, 2004 IP
  4. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Just curious, what's the code you're using to gzip the output before sending it to the browser?
    exam, Dec 5, 2004 IP
  5. blueuniverse

    blueuniverse Guest

    Messages:
    217
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    blueuniverse, Dec 5, 2004 IP
  6. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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.
    exam, Dec 6, 2004 IP
  7. blueuniverse

    blueuniverse Guest

    Messages:
    217
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Yeah, it is because I have moderation set up in order to stop the spamming from various robots. It has been moderated now :)
    blueuniverse, Dec 6, 2004 IP
  8. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Ok, sorry for submitting the comment twice. :eek:
    exam, Dec 6, 2004 IP
  9. nevetS

    nevetS Evolving Dragon

    Messages:
    2,544
    Likes Received:
    211
    Best Answers:
    0
    Trophy Points:
    135
    #9
    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.
    nevetS, Dec 6, 2004 IP
  10. blueuniverse

    blueuniverse Guest

    Messages:
    217
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Yeah, that is probably the best solution, but for those on hosting where you have snobby admins, this works best.


    EDIT -

    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.
    blueuniverse, Dec 7, 2004 IP
  11. Davidf25sc

    Davidf25sc Peon

    Messages:
    165
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #11
    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?
    Davidf25sc, Sep 26, 2006 IP
  12. adrianctn

    adrianctn Member

    Messages:
    64
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    48
    #12
    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)
    Code (Text):
    1.  
    2. Options +FollowSymLinks
    3.  
    4. RewriteEngine On
    5.  
    6. RewriteRule ^([^.]+).html$ /$1.php [QSA,L]
    7.  
    That's it. Regards,

    Adrian
    adrianctn, Sep 28, 2006 IP
    WebTechnocrat likes this.
  13. brokensoft

    brokensoft Active Member

    Messages:
    214
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    55
    #13
    what's the code you're using to gzip the output ?
    brokensoft, Oct 11, 2006 IP
  14. salik

    salik Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    <? RewriteRule ^page-name\.html $ page-name.php [T=application/x-httpd-php,L]?>


    where i place this code?
    salik, Jan 21, 2007 IP
  15. salik

    salik Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    how i can change .php files to .html ??plz tell me the simple and easier way ??
    salik, Jan 21, 2007 IP
  16. Old Welsh Guy

    Old Welsh Guy Well-Known Member

    Messages:
    2,699
    Likes Received:
    291
    Best Answers:
    0
    Trophy Points:
    155
    #16
    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.
    Old Welsh Guy, Jan 22, 2007 IP
  17. bioss

    bioss Active Member

    Messages:
    58
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    61
    #17
    PHP:
    1. RewriteEngine on
    2. RewriteBase /
    3. RewriteRule ^(.*)\.html$ $1.php [L]
    4.  
    this work for you
    bioss, Apr 8, 2008 IP
  18. Yankee85

    Yankee85 Peon

    Messages:
    1,072
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #18
    thanks bioss!
    Yankee85, Apr 21, 2008 IP
  19. chinmay

    chinmay Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #19
    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
    chinmay, May 21, 2009 IP
  20. jnugroho73

    jnugroho73 Peon

    Messages:
    77
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #20
    Try to use the following code:
    Code (Text):
    1.  
    2. RewriteEngine on
    3. RewriteCond %{HTTP_HOST} ^kusal.com [NC]
    4. RewriteRule ^(.*)$ http://www.kusal.com/$1 [L,R=301]
    5.  
    6. RewriteCond %{REQUEST_URI} ^(.*)\.php$
    7. RewriteRule ^(.*) /$1.html [R=301,L]
    8.  
    jnugroho73, May 25, 2009 IP