Removing the .php extension with mod_rewrite

.htaccess file: add the following lines:

Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

https://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/

Now you would have to check for the AllowOverride condition in apache2.log file: set options AllowOverRide All in your web root directory:

Options Indexes FollowSymLinks  
AllowOverride All  
Require all granted

If you try to access your webpage without the .php extension, you may run into the following error in your apache error.log file: /var/www/html/web/.htaccess: Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configuration

To fix this: You don't have the mod_rewrite module installed, To install it: Run the following command: ln -s /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled/rewrite.load

.htaccess: Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configuration

Again, after refreshing the webpage, you might get the folloowing error in error.log file:

[negotiation:error] [pid 4650] [client 127.0.0.1:46461] AH00687: Negotiation: discovered file(s) matching request: /var/www/html/web/test (None could be negotiated). (I was trying to access localhost/web/test.php using the url: localhost/web/test)

To fix it you would have to add the following lines in apache2.conf:

<IfModule mod_mime.c>
    AddType application/x-httpd-php .php
    AddType application/x-httpd-php .phtml
    AddType application/x-httpd-php .php3
    AddType application/x-httpd-php .php4
    AddType application/x-httpd-php .html
    AddType application/x-httpd-php-source .phps
</IfModule>

https://forums.digitalpoint.com/threads/htaccess-addtype-application-x-httpd-php-php-htm-html-and-maybe-a-fix.7584/#post-2491687

Now, I can access the test.php file using just the filename: test


RewriteEngine On
RewriteRule something something.php [L]

http://example.com/something will be handled as if it was a request for something.php

To redirect all requests that are not a physical file to the same name but with .php:

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

The accepted answer above does not remove the .php extension from urls it just allows you to visit .php files without extension. To remove the .php extension completely from urls , you can use the following Rules in root/.htaccess :

RewriteEngine on

#1)externally redirect "/file.php" to "/file"   
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
#2)Internally map "/file" back to "/file.php"
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,L]

Probably what you really want is just a way to not have the suffix at all. The best way I've found of doing this is to use the Files directive in .htaccess or in apache configuration files:

<Files myscript>
   SetHandler cgi-script
</Files>

This avoids some of the downsides of rewrites, e.g., direct access of the .php file.