I've the next htaccess file, which hides the php extension and forces all Web addresses to make use of www:

<IfModule mod_rewrite.c>
   Options +FollowSymLinks
   Options +Indexes
   RewriteEngine On
   RewriteCond %{SCRIPT_FILENAME} !-d
   RewriteRule ^([^\.]+)$ $1.php [NC,L]
</IfModule>

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

For part one the loan would go to this answer and also the second someone to this answer.

This works perfect generally, however there's one problem. Whenever a user tries to visit http://example.com/foo (or rather than "foo", whatever other page) it redirects to http://www.example.com/foo.php .

How do i allow it to be redirect to http://www.example.com/foo , that's, whithout the .php?

You need to simply alter the order of the rules. First exterior redirect then your internal one. Have your code such as this:

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

<IfModule mod_rewrite.c>
   Options +FollowSymLinks
   Options +Indexes
   RewriteEngine On
   RewriteCond %{SCRIPT_FILENAME} !-d
   RewriteRule ^([^\.]+)$ $1.php [NC,L]
</IfModule>

I've examined it and works fine on my small finish.

What's "foo" within this situation, personal files? If that's the case, this works:

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]

If you wish to exclude "foo" particularly,

RewriteRule ^foo$ - [L]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]

If you are trying to utilize a web framework, they most likely would like you to redirect all non-file and non-directory demands to something similar to index.php.

Edit: Try changing and bringing together the 2 sections:

Options +FollowSymLinks
Options +Indexes

<IfModule rewrite_module>
  RewriteEngine On
  RewriteCond %{HTTP_HOST} !^www\.
  RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
  RewriteCond %{SCRIPT_FILENAME} !-f
  RewriteCond %{SCRIPT_FILENAME} !-d
  RewriteRule ^([^\.]+)$ $1.php [NC,L]
</IfModule>