我正在尝试构建custom PHP MVC framework
,我想使用.htaccess
重写执行以下操作
localhost/mvc/page1
或localhost/mvc/page1/
对此localhost/mvc/index/view/page1
与
localhost/mvc/blog
或localhost/mvc/blog/
对此localhost/mvc/blog/index
与
localhost/mvc/blog/blog1
或localhost/mvc/blog/blog1/
对此localhost/mvc/blog/view/blog1
这里index
和blog
是控制器
以下是我迄今为止尝试的
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?rt=index/view/$1 [L,QSA]
这会将localhost/mvc/page1
重定向到localhost/mvc/index/view/page1
所以,基本上,我想对不同的控制器有不同的重写条件。所以,当我将添加一个新的控制器时,我将继续向htaccess文件添加条件。
这是一个代码隐藏重定向(URL不应更改),。CCD_ 16后面的所有内容都应该反映在GET $_GET['rt']
参数中
如何做到这一点??
这样做:
RewriteEngine on
RewriteBase /mvc/
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(blog)/?$ index.php?rt=$1/index [L,NC,QSA]
RewriteRule ^(blog)/([^/]+)/?$ index.php?rt=$1/view/$2 [L,NC,QSA]
RewriteRule ^(.+)$ index.php?rt=index/view/$1 [L,QSA]