Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess; no extension required

I need some .htaccess code that will treat extensionless files as PHP files.

Suppose the visitors visits www.mywebsite.com/dir1/dir2/file.name, it will first look the file dir1/dir2/file.name exists and if it not exists, it will look for dir1/dir2/file.name.php (so with .php extension).

Is that possible in some way?

like image 560
www.data-blogger.com Avatar asked Jun 28 '11 15:06

www.data-blogger.com


People also ask

How do I remove page file extension from URL?

The . html extension can be easily removed by editing the . htaccess file.

What is .htaccess file in PHP?

htaccess file is created in order to enable extra features for that subdirectory. You can use the . htaccess file to modify various configurations and thus make changes to your website. These changes include authorization, error handling, redirects for specific URLs, user permissions, etc.


2 Answers

You can write a rewriting rule which only takes into effect if the requested file doesn't exists.

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !\.php$
RewriteRule ^(.*)$ $1.php [L]
like image 94
Andrew Moore Avatar answered Sep 20 '22 03:09

Andrew Moore


You should have a look at Apache's Options directive, most specifically the http://httpd.apache.org/docs/2.0/content-negotiation.html option which you can include in your .htaccess file and will do just that.

Add to your .htaccess:

Options +MultiViews
like image 40
Francois Deschenes Avatar answered Sep 20 '22 03:09

Francois Deschenes