我正在使用PHP构建一个应用程序,并且想知道如何将像"http://localhost/app/index.php?function=viewAssignments&id=14 这样的URL变成类似 http://localhost/app/assignments/14 的东西,而不必为每个任务创建一个名为assignments的文件夹和页面。 (这显然是不现实的)。
我已经学会了如何更改 htaccess 以使单个 PHP 文件可以访问,而无需在最后实际键入.php,但是有没有办法像我提到的那样制作干净的 URL?
提前感谢您的帮助!
假设您的服务器支持 mod_rewrite,那么您只需在目录中创建一个名为 .htaccess
并将此代码复制到文件中即可实现此目的,
RewriteEngine On
RewriteRule ^app/([^/]*)/([^/]*)$ /app/index.php?function=$1&id=$2 [L]
这将需要这样的东西,
http://localhost/app/index.php?function=viewAssignments&id=14
看起来像这样,
http://localhost/app/viewAssignments/14