-
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.
-
-
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
-
-
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 =/
-
-
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).
-
-
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]
-
-
I don't know about .php, but it should work for .html. It might work for .php too; give it a go.
-
The Following User Says Thank You to Twey For This Useful Post:
-
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?
-
-
It might be in your server headers somewhere, unless it's been configured not to show itself. Otherwise, read your server config.
-
-
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
Bookmarks