PHP MVC有更好的htaccessPHP、MVC、htaccess

2023-09-02 00:46:59 作者:争霸天涯

我不知道该怎么htaccess的作品,而它的理论只是不会在我的头上连接不管我有多少教程阅读。

我建立了一个简单的MVC框架,工作很漂亮,但我不喜欢我处理htaccess的方式。要重写URL的正确,这是我在做什么:

  RewriteEngine叙述上
?重写规则^用户/([^ /] +)(/([^ /] +))$控制器/ users.php方法= $ 1安培;参数= $ 3
 

如果我添加一个新的控制器,然后我必须进入htaccess的,并添加一个新行:

重写规则^接入/([^ /] +)(/([^ /] +))$控制器/ access.php方法= $ 1安培;参数= $ 3

有没有一种方法,使所有的自动带通配符的领域,所以我没有访问htaccess的我每次做一个更新?

解决方案 php mvc smarty mysql MVC框架 与Smarty

您可以试试这个:

  RewriteEngine叙述上
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
?重写规则^([^ /] +)/([^ /] +)(/([^ /] +))$控制器/ $ 1.PHP方法= $ 2及?参数= $ 4个
 

这两个额外的规则将跳过重写,如果确实引用的文件或directoy存在于磁盘,例如在:它不会试图改写为 http://site.com/images/请求logo.jpg

I have no idea how htaccess works, and the theory of it just wont connect in my head no matter how many tutorials I read.

I am building a simple MVC framework which works beautifully, except I don't like the way I am dealing with htaccess. To rewrite the URL's properly, this is what I am doing:

RewriteEngine on
RewriteRule ^users/([^/]+)(/([^/]+))?$ controller/users.php?method=$1&param=$3

If I add a new controller, I then have to go into htaccess and add a new line:

RewriteRule ^access/([^/]+)(/([^/]+))?$ controller/access.php?method=$1&param=$3

Is there a way to make it all automatic with wildcard fields so I don't have to access htaccess every time I do an update?

解决方案

You can try this:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)(/([^/]+))?$ controller/$1.php?method=$2&param=$4

The two extra rules will skip the rewrite if the file or directoy referenced actually exists on the disk, eg: it won't try to rewrite requests for http://site.com/images/logo.jpg.