MakerModelLua MakerModelLua - 1 year ago 64
PHP Question

How to remove .php File extension for a single file? (.htaccess)

I've searched the web, but I couldn't find how to hide the .php extension for a single file

(/foo.php to /foo)

How could I do this to this particular file, without touching any of the other ones, using .htaccess

(the answer is probably simple, but I'm either stupid or didn't look hard enough)

Answer Source

If you mean that you want your users to enter /foo in the browser and really get to /foo.php behind the scenes (eg: to have pretty URLs), you need an internal redirect. Add the following to .htaccess file in the site's top public folder:

RewriteEngine On
RewriteBase /

RewriteRule ^foo$    /foo.php     [END,QSA,NC]

How it works

  1. Your server receives a request for content at path /foo
  2. Server finds the rule above, and forwards the request and internally changes the path to /foo.php

  3. END flag: the request is sent to /foo.php immediately. Use L instead in old versions of Apache (see docs)

  4. QSA flag: request parameters are also forwarded (eg: /foo?item=7)
  5. NC flag: case insensitive match. /FOO will also be forwarded to /foo.php