I have a file
http://www.www.com/somefile.php
I would like to be able to get to the same file by going to
http://www.www.com/somefile
But I still need
http://www.www.com/directory
to open the file
http://www.www.com/directory/index.php
if, in fact, directory is a directory.
开发者_运维技巧This is a typical LAMP setup with Red Hat and Apache.
Is that possible? If so, how?
An apache rewrite rule will do this for you. The only limitation is you can't have a file named "somefile.php" and a directory named "somefile" (though you can still have directories.
Here's an example rule that supports multiple filenames (just put in in your .htaccess file).
RewriteEngine on
RewriteRule ^(somefile1|somefile2|somefile3) $1.php
Will change /somefile1 to /somefile1.php, /somefile2 to /somefile2.php, and /somefile3 to /somefile3.php.
Look at Apache's ForceType.
If you want to disable the need of file extensions on the url globally on your site, you can also add the following to your .htaccess
:
Options +MultiViews
This will enable Content Negotiation. But if you want to match only certain files and/or urls, stick with SoapBox's Answer instead.
精彩评论