Mod rewrite - hiding php extensions

Discussion in 'Apache' started by ollyno1uk, Dec 22, 2008.

  1. #1
    Hi there

    I have a site with a load of php extensions that I would like to make .html.

    I have looked for a solution online and found this :

    
    RewriteBase /
    RewriteRule ^(.*)\.php $1.html [R=301,L]
    RewriteRule ^(.*)\.html $1.php [L]
    
    Code (markup):
    However this results in a redirect loop.

    Is there a simple way I can rewrite the .php to .html extensions and 301 the old to the new?

    Thanks
     
    ollyno1uk, Dec 22, 2008 IP
  2. pcknowhow.com

    pcknowhow.com Peon

    Messages:
    47
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    RewriteEngine On
    RewriteRule ^(.*)\.php$ http://www.domain.com/$1.html [R=301,L]
     
    pcknowhow.com, Dec 22, 2008 IP
  3. ollyno1uk

    ollyno1uk Active Member

    Messages:
    946
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    70
    #3
    thanks for this.

    I now get an error to say that the html file is not found.
     
    ollyno1uk, Dec 22, 2008 IP
  4. pcknowhow.com

    pcknowhow.com Peon

    Messages:
    47
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Ah sorry, I misunderstood what you wanted to do last time.

    I think this might do the trick.

    RewriteEngine On 
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^(.*)\.php$ /$1.html [R=301,L]
    RewriteRule ^(.*)\.html$ /$1.php [L]
    Code (markup):
     
    pcknowhow.com, Dec 22, 2008 IP
  5. ollyno1uk

    ollyno1uk Active Member

    Messages:
    946
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    70
    #5
    You are a genius - this is perfect.

    Can I ask what the ENV:REDIRECT_STATUS actually does so I can understand it?
     
    ollyno1uk, Dec 23, 2008 IP
  6. pcknowhow.com

    pcknowhow.com Peon

    Messages:
    47
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    The problem before was when you requested say test.html, Apache would process the .htaccess file and make a sub request for test.php. At this point .htaccess is processed again, and because test.php matches the rewrite rule, it's redirected to test.html, and the loop will start again!

    REDIRECT_STATUS is a server variable. If a sub request is made, this is the HTTP response status code of the original request, otherwise it'll be empty. Therefore you can test against this to avoid a loop.

    This condition is checking that REDIRECT_STATUS is empty:
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    Code (markup):
    I hope this helps somewhat.
     
    pcknowhow.com, Dec 23, 2008 IP
    ollyno1uk likes this.
  7. ollyno1uk

    ollyno1uk Active Member

    Messages:
    946
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    70
    #7
    It helps a lot - thanks again for superb help

    Rep added
     
    ollyno1uk, Dec 23, 2008 IP