title: .htaccess how to hide php extension
body: <p>Let's suppose I have <code>.htaccess</code> file at the wwwroot of my Apache website, say, <code>www.example.com</code>.</p>
  
  <p>I want to achieve this effect:</p>
  
  <blockquote>
    <p>When the user input the link <code>www.example.com/dashboard</code> in the
    browser, it will actually show content in
    <code>www.example.com/dashboard.php</code></p>
    
    <p>And if the user directly input the link
    <code>www.example.com/dashboard.php</code> in the browser, it will show error.</p>
  </blockquote>
  
  <hr>
  
  <p>I've tried the following <code>.htaccess</code> code:</p>
  
  <pre><code>RewriteEngine on
  
  # this is to deny direct access of php files in browser
  # that is, when the user input directly 'www.example.com/dashboard.php'
    it will turn to error
  RewriteRule \.php$ - [F,L]
  
  
  # if the link the user input to the browser does not end with .php,
    the code will go down ahead
  
  # this rewrite 'www.example.com/dashboard' to 'www.example.com/dashboard.php'
  RewriteCond %{REQUEST_FILENAME}.php -f
  RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L,END]
  </code></pre>
  
  <p>However this will make <code>www.example.com/dashboard</code> inaccessible from the browser as well.</p>
  
  <p>If I remove the <code>RewriteRule \.php$ - [F,L]</code> line, things are working fine but the user can still input <code>www.example.com/dashboard.php</code> in the browser to directly access the php page.</p>
  
  <hr>
  
  <p>I'm still confused how <code>.htaccess</code> files are actually processed by Apache.</p>
  
  <p>Is that a flow that executes the code from the beginning to the end line by line? Or something else?</p>
  
  <hr>
  
  <p><strong>UPDATE</strong></p>
  
  <p>And this gives me <strong>ERR_TOO_MANY_REDIRECTS</strong>：</p>
  
  <pre><code>RewriteEngine on
  
  RewriteRule ^/?(.+)\.php$ /$1 [R=301]
  
  RewriteCond %{REQUEST_FILENAME} !\.php$
  RewriteCond %{REQUEST_FILENAME}.php -f
  RewriteRule ^/?(.+)$ /$1.php [END]
  </code></pre>
  
  <p>I'm quite confused can anyone help explain why this gives too many redirects</p>