使用htaccess将带/不带.php扩展名的URL重定向到index.php

I use Shephertz's very basic URL routing mechanism to route my page. However I have come across a problem for file extensions.

If I load www.example.com/register I get index.php, but keep the URL.

However, if I load up www.example.com/register.php, it doesn't send me to index.php, but keeps the URL. How come?

This is my .htaccess file, which works for non-extensions:

Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

How can I combine these two? I want both of these to be marked blue...

To redirect all external php requests to 404 , You can use the following Rule in .htaccess :

RewriteCond %{THE_REQUEST} /.+\.php [NC]
RewriteRule ^ - [L,R=404]

Solved this myself by using RewriteCond and RewriteRule. This is what my .htaccess looks like now:

Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} /.+\.php [NC]
RewriteRule ^ index.php [QSA,L]

I can now redirect all URL's with .php file extension to, a custom 404 page, using Shephertz's routing mechanism.

  • www.example.com/example.php --> 404page.php
  • www.example.com/example --> example.php

This is the 404 page:

enter image description here

This is page without .php file extension

enter image description here