6

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?

Toby Allen
  • 10,997
  • 11
  • 73
  • 124
www.data-blogger.com
  • 4,076
  • 7
  • 43
  • 65
  • `MultiViews` is the easiest option here. It incurs only minimal directory reading overhead (and brings some other advantages), so a more specific RewriteCond/RewriteRule might be unnecessary. – mario Jun 28 '11 at 15:44
  • @mario: `MultiViews` enables that behaviour for **all filetypes**, not just PHP files. That might be undesirable. Unless you use `MultiviewsMatch` as well. – Andrew Moore Jun 28 '11 at 15:46
  • @AndrewMoore: That's what I referred to with "some other advantages". It can be enabled/disabled on a per-directory basis however, and even via FilesMatch iirc. – mario Jun 28 '11 at 15:48
  • Ok, no it's just desirable. But `index.php` (when I go to `http://www.mywebsite.com/`) gives me an Internal Server error now. What can I do about that? – www.data-blogger.com Jun 28 '11 at 15:49
  • Look into the `error.log` which will mention the actual conflict / error cause. You shouldn't have to combine both approaches btw. -- Your question might now be better suited for [Serverfault](http://serverfault.com/) - flag for moderator attention if you want it moved. – mario Jun 28 '11 at 15:50
  • Ah: Request exceeded the limit of 10 internal redirects due to probable configuration error. So I guess it has something to do with the RewriteCond I added (from Andrew). – www.data-blogger.com Jun 28 '11 at 15:54
  • @Kevin - That may make sense actually since if the file is not found, it'll keep adding `.php` at the end. For instance, `index.php` will become `index.php.php` and then `index.php.php.php` and so on and so forth. @Andrew Moore should rewrite it so that if .php is already at the end it doesn't keep adding (via another `RewriteCond`). – Francois Deschenes Jun 28 '11 at 15:57

3 Answers3

10

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]
Andrew Moore
  • 93,497
  • 30
  • 163
  • 175
  • W00t, awsome! Need to notice that I needed `Francois Deschenes` answer too. – www.data-blogger.com Jun 28 '11 at 15:46
  • 1
    Wouldn't you rather check that the php file exists instead of checking whether the requested file does not exist? The current answer may break the directory index by redirecting `/dir/` to `/dir/.php`. – erjiang Jul 15 '14 at 14:04
4

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
Francois Deschenes
  • 24,816
  • 4
  • 64
  • 61
  • MultiViews has the problem of enabling that type of behaviour for **all filetypes**, not just PHP files. – Andrew Moore Jun 28 '11 at 15:44
  • Thanks! Your answer and `Andrew Moore's` answer together worked for me. – www.data-blogger.com Jun 28 '11 at 15:46
  • Mpf, `index.php` gives an Internal Server error now. What can I do about that? – www.data-blogger.com Jun 28 '11 at 15:48
  • @Kevin - It sounds like you don't have the permission to change the `Options` setting in your `.htaccess` file. If you own the server and can change the Apache configuration file, you'll need to give yourself permission otherwise, try @Andrew Moore's suggestion using `mod_rewrite`. To change permissions in your httpd.conf file, you'll want to use [`AllowOverride`](http://httpd.apache.org/docs/2.0/mod/core.html#allowoverride) and set it to `All` or add `Options`. – Francois Deschenes Jun 28 '11 at 15:51
  • No, it already is in the Apache config file (with mod_rewrite enabled). I guess it has something todo with the Andrews rules I used in the `.htaccess` file. And `AllowOverride` is set to `All`. – www.data-blogger.com Jun 28 '11 at 15:52
  • @Kevin - Internal server errors that happen after you changed your `.htaccess` are usually caused by errors (or lack of permissions) in the `.htaccess` file. Check your error.log. – Francois Deschenes Jun 28 '11 at 15:54
0

Ok, the answer was a combination of answers. I needed to add Options +MultiViews in my Apache configuration. Then I extend Andrews answer to the following:

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ $1.php [L]

Regards and many thanks! Kevin

www.data-blogger.com
  • 4,076
  • 7
  • 43
  • 65
  • **You should only need one or the other... not both.** @Andrew Moore does exactly what `Options +MultiViews` does but only for `.php` files. – Francois Deschenes Jun 28 '11 at 16:02