My .htaccess file is as follows:
Options -Multiviews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php
The issue that I am having is that when I try to access my site without the www. prefix, the .php extension is added to the address, which can often cause a problem. For example, if I try to access my homepage with the address example.com, that address is transformed into www.example.com/.php. I want the www. to be added, but the .php extension added at the end just causes an error. How do I fix this?
I believe the problem is with your last line. Mod Rewrite does not stop after executing the first rule. Try adding the last rule modifier to the redirect rule:
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
You need to tell apache to stop processing rules after the 301 redirect, do this by adding an L - e.g.:
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
aside: you'd get on better if you enabled multiviews on the first line, and removed the last 3 lines ;)
Remove below line from .htaccess and try..
RewriteRule ^(.*)$ $1.php