htaccess rewrite html to php

Discussion in 'Site & Server Administration' started by hasen, Mar 15, 2009.

  1. #1
    I have my php site set up so the urls appear as .html but rewrite to .php behind the scenes. But, what I want it also to do is if someone does manually type in the urls with .php that they redirect/rewrite as html. So in other words, the .php pages are never displayed as .php under any circumstances.

    So mydomain.com/page.php manually entered would be redirected/rewritten as mydomain.com/page.html

    The code I currently use in my htaccess is:

    
    RewriteRule ^(.*)\.html$ $1.php [NC]
    
    Code (markup):
    Which works fine to rewrite all the .html urls internally to .php but doesn't prevent people manually entering .php urls which I don't want.

    I hope you get what I mean. Thanks in advance.
     
    hasen, Mar 15, 2009 IP
  2. InFloW

    InFloW Peon

    Messages:
    1,488
    Likes Received:
    39
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    <Files ~ "^\.php">
    Order allow,deny
    Deny from all
    </Files>
    
    Code (markup):
    I think that'll do it. Just put it in your .htaccess file.
     
    InFloW, Mar 15, 2009 IP
  3. hasen

    hasen Peon

    Messages:
    994
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Won't that just prevent them from accessing the .php urls completely? I want them to redirect to the .html versions of the pages not just block anyone.
     
    hasen, Mar 15, 2009 IP
  4. InFloW

    InFloW Peon

    Messages:
    1,488
    Likes Received:
    39
    Best Answers:
    0
    Trophy Points:
    0
    #4
    It would prevent them from accessing it completely. You might be able to create some sort of weird Redirect using mod_rewrite. I am just not sure how well that would work out or how you'd do it without creating loops.

    It might be something like this:

    
    RewriteRule ^(.*)\.php$ http://yourdomain.com/$1.html [R=301,L]
    
    Code (markup):
    You'd probably have to put it above the .html one. I haven't tested it myself but this should point you in the right direction.
     
    InFloW, Mar 15, 2009 IP
  5. hasen

    hasen Peon

    Messages:
    994
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #5
    That wouldn't work 'cos I'd have to change the url for every page. I just want to redirect every .php page to the .html equivalent. I guess no-one knows how to do it.
     
    hasen, Mar 16, 2009 IP
  6. Dimma

    Dimma Well-Known Member

    Messages:
    631
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    140
    #6
    OK, here is the solution for you

    RewriteRule ^(.*)\.html$ $1\.php [E=FLAG1:1,L]
    RewriteCond %{ENV:REDIRECT_FLAG1} !1
    RewriteRule ^(.*)\.php$ $1\.html [R=301]
     
    Dimma, Mar 17, 2009 IP
  7. hasen

    hasen Peon

    Messages:
    994
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Dimma that's almost it but it doesn't quite work. If I try to query domain.com/page.php it redirects to domain.com/home/domain/public_html/page.html which obviously doesn't work.

    Update: You put me on the right track, Dimma and I managed to find the solution. For anyone wondering its:

    RewriteRule ^(.*)\.html$ $1.php [NC]
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /[^.?]+\.php
    rewriterule ^([^.]+)\.php$ http://www.domain.com/$1.html [R=301,L]
     
    hasen, Mar 19, 2009 IP