Routing URL Path with PHP and Apache

给你一囗甜甜゛ 提交于 2019-12-04 16:38:38

For a quick way to handle Front-end Controllers with Apache, use FallbackResource and parse the URL with PHP.

FallbackResource /index.php

htaccess should be something like this

RewriteEngine on
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)$ $1.php?$2=$3 [QSA]

so for example

/home/something/value would be redirected to /home.php?something=value

Give it a go, not completely sure on this but have done something similar before.

From a PHP perspective, you would just need to adjust your init class to key off of $_SERVER['REQUEST_URI'] instead of $_SERVER['QUERY_STRING']. You are not showing your full logic here, but my guess is that you would simply trim the first / and then explode the URI on / to get at the component parts.

From an .htaccess standpoint, you could just change your last line to be:

RewriteRule ^.*$ index.php [L,QSA]

You don't need to specifically detect images/css/js here as that RewriteCond already makes exceptions to the rewrite rule for any actual files or directories. Note that in the RewriteRule I did leave the Query String Append (QSA) flag in place in case you still wanted to do something like /controller?some=string&of=data If you don't anticipate using query strings at all with your new URI's, then you can omit this flag.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!