title: Using mod_rewrite to hide .php from the end of URLs
body: <p>I've got a site where all the pages are php scripts, so the URLs end .php.</p>
  
  <p>I've added the following to a .htaccess file, and I can now access the .php files without the .php extension:</p>
  
  <pre><code>RewriteEngine On  # Turn on rewriting
  
  RewriteCond %{REQUEST_FILENAME}.php -f  # If the requested file with .php on the end exists
  RewriteRule ^(.*)$ $1.php #  serve the PHP file
  </code></pre>
  
  <p>So far so good. But now I want to add a Redirect on all the .php files so that any old links outside of my control get redirected to the new version of the URL.</p>
  
  <p>I've tried this:</p>
  
  <pre><code>RewriteEngine On  # Turn on rewriting
  
  RewriteCond %{REQUEST_URI} .*\.php
  RewriteRule ^(.*)\.php$ http://example.com/$1 [R=permanent,L]
  
  RewriteCond %{REQUEST_FILENAME}.php -f  # If the requested file with .php on the end exists
  RewriteRule ^(.*)$ $1.php [L] #  serve the PHP file
  </code></pre>
  
  <p>but that seems to send a redirect even for URLs that don't end in .php, so I get stuck in an infinite loop. Any other combination I try seems to match no requests (and leave me at page.php) or all requests (and get me stuck in a loop).</p>