Say I have a page testpage.php and I want to access it through localhost/testpage . I can't figure out the regex to get it to work.
When I use the following, I get a server error
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* "^[^0-9][A-z0-9_]+*.php"/$0
However, when I specify the exact file it works.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* testpage.php/$0
Try this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
the third line checks if the file actually exists before doing the rewrite to avoid errors
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php$
RewriteRule ^(.*)$ $1.php [L]
It will rewrite the filename with .php extention
You cannot specify a regular expression in the replacement pattern. That's why it fails. In this line
RewriteRule .* "^[^0-9][A-z0-9_]+*.php"/$0
You are telling Apache to rewrite anything ( .*
) to the literal "^[^0-9][A-z0-9_]+*.php"/
, plus the zeroth matched string (which is an error anyway, because matches start from $1, then there is $2 etc.).
If you want to capture a match you must surround it with parentheses, like
(.*)
and then it will be available in the replacement string as $1
.