Rio
Rio

Reputation: 14892

Default to PHP extension on web server

Is there a way a .htaccess file can be written so that

http://www.mysite.com/about 

loads about.php

and the same goes for any file?

I tried

RewriteEngine On
RewriteRule ^([0-9A-Za-z]+)$ $1.php

but it doesn't seem to work.

Upvotes: 2

Views: 5918

Answers (1)

LazyOne
LazyOne

Reputation: 165288

This is the most common rule (utilising mod_rewrite -- make sure it is loaded and enabled) -- it will ensure that such .php file does exist before rewriting (yes, it's a bit slower but safer):

Options +FollowSymLinks
RewriteEngine On

# add .php file extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)$ $1.php [L]

Alternatively just use this:

Options +MultiViews

This will turn on "content negotiation". But it has some cons.

For example: let's assume you have hello.html & hello.php in your website root folder. If you request example.com/hello, with that option enabled Apache will look for alternative names (same name but different extensions) and will serve either hello.html or hello.php (I cannot tell which one will be preferred).

But if you only have 1 file with such unique name (e.g. hello.php ONLY) then no problems here at all.

Upvotes: 3

Related Questions