Search code examples
phpapache-config

Is it possible to have a php file in apache operate without an extension?


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?


Solution

  • 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.