如何删除 .php 并替换?使用 / 在 htaccess 中

How to remove .php and replace ? with / in htaccess

如何删除 .php 并同时将 ? 替换为 /

我有

http://localhost/testingproject/testing.php?mode=getdata

我希望它像这样从浏览器调用:

http://localhost/testingproject/testing/getdata

在替换 ? 数据时,我希望它与目录兼容。

就像在目录中一样,我有:

testing.php
user.php
dashboard.php

因此,如果用户单独调用 http://localhost/testingproject/user,则无需将 ? 解析为 /,因为 user 是有效的 PHP文件:user.php.

但是如果用户调用 http://localhost/testingproject/user/abc,那么 user.php 页面应该得到 $_GET['mode']abc

我已搜索但仍找不到兼容的 htaccess 代码。

我找到了一个有效的方法。 现在,我可以调用 http://localhost/testproject/backend/user/abc 而不是 http://localhost/testproject/backend.php?mode=abc 在Replace GET variables with slashes .htaccess

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^([^/]+)/?$ .php [L]

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^([^/]+)/([^/]+)/?$ .php?mode= [L,QSA]