Mod_rewrite Rewringting URL without file extensions (.html)

Discussion in 'Apache' started by lonewalker, May 22, 2007.

  1. #1
    File structure: /www/file.(php|html) , /www/folder/file.(php|html)

    Is it possible to to loose the .php and .html in the url?
    In other words rewrite the URL to access it from http://localhost/file and http://localhost/folder/file
     
    lonewalker, May 22, 2007 IP
  2. lemaitre

    lemaitre Peon

    Messages:
    61
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try this:

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.*)$ $1.php
    RewriteCond %{REQUEST_FILENAME}.html -f
    RewriteRule ^(.*)$ $1.html
    Code (markup):
    There is an ambiguous case when you request http://example.com/file and both file.html and file.php exist. This rule will choose file.php.
     
    lemaitre, May 22, 2007 IP
  3. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #3
    You can only do something close to this if you have something unique in the URLs so apache knows if the page is from .html or .php.

    For example domain.com/whatever/ can only point to html OR php.

    Options +FollowSymLinks +Indexes
    RewriteEngine on
    RewriteBase /
    RewriteRule ^folder/([^.]+)/$ folder/$1.php [L]
    RewriteRule ^([^.]+)/$ $1.php [L]

    to get rid of the php.
     
    Nintendo, May 22, 2007 IP
  4. lonewalker

    lonewalker Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hey thanks guys it works :)
     
    lonewalker, May 23, 2007 IP