Results 1 to 9 of 9

Thread: Removing file extensions with htaccess

  1. #1
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default Removing file extensions with htaccess

    Ok the title is a bit ambiguous, but I really don't know the word to sum it up.

    I want to make a site that use www.something.com/bla

    Instead of www.something.com/bla.html

    I know this can be achieved by making a new directory called "bla" and then putting a .index.html inside that directory, but I've seen on some sites where the index.html doesn't exist.

    I did try searching for an answer but I just didn't really know what to search for or how you'd describe it. I know I've seen a thread about it before, anyone got any ideas?

    Edit: Better example - http://en.wikipedia.org/wiki/Hello - No extension there. If you try adding /index.html or .php or .asp etc etc the file still isn't found. This should clarify what I want to find out.
    Last edited by Schmoopy; 02-22-2009 at 03:23 AM.

  2. #2
    Join Date
    Oct 2008
    Location
    Sweden
    Posts
    2,023
    Thanks
    17
    Thanked 319 Times in 318 Posts
    Blog Entries
    3

    Default

    I think there are at least two ways you can accomplish this. One would be to make it a folder; the default directory file is not always index.ext or default.ext, you can set up the server to open any file you want when the user only types the folder using .htaccess. Take a look at these articles:
    http://www.besthostratings.com/articles/change-default-page.html
    http://roshanbh.com.np/2008/04/change-default-page-htaccess.html

    The second way would be to use .htaccess to remove the file name or the extension from the file name so /foo.html would become /foo. These should get you started:
    http://www.htaccesselite.com/remove-php-extensions-htaccess-novice-vt339.html
    http://www.webmasterworld.com/forum92/6453.htm

    Good luck!
    Last edited by Snookerman; 02-17-2009 at 08:59 PM. Reason: added links
    Eddy Proca
    I love Dropbox. Get it through my girlfriend's referral link (I reached my limit) and both you and her get 500 MB extra! Thanks and you're welcome!

  3. #3
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    Ok, I've done that now, and it's working fine. Only thing is pages that should come up with 404 errors come up with "Internal Server Error", I mean it's not a huge thing but it's a bit ugly =/

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    If you have mod_negotiation, you can simply enable Options +MultiViews in your .htaccess to enable this behaviour (there's more to MultiViews, though: you should look it up if you have a multilingual or otherwise somewhat complex site).
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    How would I find out if mod_negotiations is enabled or not? And do you mean just write Options +MultiViews instead of in .htaccess:

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ $1.php [L,QSA]

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I don't know about .php, but it should work for .html. It might work for .php too; give it a go.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. The Following User Says Thank You to Twey For This Useful Post:

    Schmoopy (02-22-2009)

  8. #7
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    Ah nice, yup - it works with .php extensions. Just for future reference where do you look to see if mod_negotiation is enabled?

    I've found it if I do phpinfo(), but if you're just running HTML pages how would you find it?

  9. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    It might be in your server headers somewhere, unless it's been configured not to show itself. Otherwise, read your server config.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  10. #9
    Join Date
    Aug 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Remove file extension

    If you are using a Linux server or hosting you can try placing this code in your .htaccess file.

    Options -MultiViews
    RewriteEngine On
    RewriteBase /
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
    RewriteRule ^ %1 [R,L,NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ $1.php [L]

    -FYI when writing code for .htaccess files using windows wordpad or notepad be sure to turn off wordwrap. These text editors place characters not recognizable by servers and if wordwrap is on your .htaccess file may not work.

    Here are some other configurations to consider:
    .htaccess files are a simple ASCII text file with the name .htaccess. It is not an extension like .html or .txt. The entire file name is .htaccess. For more information on how to set up .htaccess files, visit Apache's website.
    Last edited by jscheuer1; 08-17-2012 at 02:35 PM. Reason: remove self promotion

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •