title: How-to? Vanity URL & ignore .PHP extension with .htaccess
body: <p>I've got the following .htaccess working perfectly:</p>
  
  <pre><code>RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} -f [OR]
  RewriteCond %{REQUEST_FILENAME} -d
  RewriteRule .* - [L]
  RewriteRule ^(.*)$ http://www.mysite.com/mypage.php?u=$1 [NC]
  </code></pre>
  
  <p>This will allow me to write urls like this: </p>
  
  <pre><code>http://www.mysite.com/peter  =&gt;  http://www.mysite.com/mypage.php?u=peter
  </code></pre>
  
  <p>But what if I want have an aditional rule to avoid writing the .php extension for existing files?</p>
  
  <pre><code>http://www.mysite.com/contact   (contact.php exists on file)
  </code></pre>
  
  <p>I tried adding this to my curr htaccess (but doesnt work):</p>
  
  <pre><code>RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME}\.php -f
  RewriteRule ^(.*)$ $1.php
  </code></pre>
  
  <p>In other words, I would like to have vanity urls and ignore the .php extension in the same .htaccess.</p>
  
  <hr>
  
  <p>SOLVED! 
  Thanks to Ulrich Palha and Mario (didnt know about the ordering and the LAST [L] flag!</p>
  
  <pre><code>RewriteEngine on
  # Unless directory, remove trailing slash
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^([^/]+)/$ http://www.mysite.com/$1 [R=301,L]
  
  # Redirect external .php requests to extensionless url
  RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
  RewriteRule ^(.+)\.php$ http://www.mysite.com/$1 [R=301,L]
  
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME}\.php -f
  RewriteRule ^(.*)$ $1.php [L]
  
  RewriteCond %{REQUEST_FILENAME} -f [OR]
  RewriteCond %{REQUEST_FILENAME} -d
  RewriteRule .* - [L]
  RewriteRule ^(.*)$ http://www.mysite.com/mypage.php?u=$1 [NC]
  </code></pre>