Udders
Udders

Reputation: 6976

Apache request page without PHP extension

Is it possible to request a file with it's .php extension, so for example, instead of going to, http://example.com/about.php the user would only need to type http://example.com/about.

I have tried the following but just get a 404 page,

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]

Upvotes: 0

Views: 602

Answers (2)

Cybercartel
Cybercartel

Reputation: 12592

You need to exclude .php from the url because otherwise you can get an infinite loop and it result in an url like this .php.php.php.php. But the idea is good and it works.

  1. How do I remove the '.php' extension from URLs?

Upvotes: 2

Salman Arshad
Salman Arshad

Reputation: 272106

Your rules should work if the requested file (after rewriting) exists. For example the page /phpinfo was requested and the file /phpinfo.php exists. But the same rules will create an infinite loop if the file (after rewriting) does not exist, for example:

  • /phpinfo matches the RewriteRule and both RewriteCond are satisfied, result will be /phpinfo.php
  • L flag stops current iteration but since filename has changed, another iteration is required
  • /phpinfo.php matches the RewriteRule and both RewriteCond are satisfied, result will be /phpinfo.php.php
  • repeat until mod_rewrite gives up

There are few workarounds, the simplest one is to tweak your RewriteCond:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*) $1.php [QSA,L]

If you continuously get 404 error even for simple rewrite rules, chances are that your rewrite rules are not being processed by Apache. A possible reason is that that AllowOverride is set to None in server configuration file (httpd.conf). Change that to AllowOverride FileInfo and restart Apache.

Upvotes: 1

Related Questions