在独立项目上使用 .htaccess 进行路由和 SEFURL


Routing and SEFURL with .htaccess on an independent project

我开发了一个小的(功能强大且轻量级的:-D(php MVC框架

我目前遇到的问题是:路线遵循此顺序

index.php?option=samplemodule&action=sampleController

这将链接到一个samplemodule并执行sampleController,就像zf前段时间所做的那样。

我正在尝试让 .htaccess 将规则重写为/模块/操作

但由于某种原因它不起作用,这是一段代码

RewriteEngine On    # Turn on the rewriting engine
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule     ^[A-Za-z0-9-]+/([A-Za-z0-9-]+)/?$ index.php?option=$1&action=$2 [NC,L]
RewriteRule     ^[A-Za-z0-9-]+/([A-Za-z0-9-]+)?$ index.php?option=$1&action=$2 [NC,L]
RewriteRule     ^([A-Za-z0-9-]+)/?$ index.php?option=$1&action=main [NC,L]
RewriteRule     ^([A-Za-z0-9-]+)?$ index.php?option=$1&action=main [NC,L]

处理正则表达式非常困难

我也希望,在传递变量时,这些可以是/module/action/var1/value1/var2/value2

但我不确定重写模式引擎是如何工作的......它是否递归地使用多个规则?

它是否递归地使用多个规则?

确实如此,但并不像听起来那么简单。

至于可以无限期存在的 URL 模式,例如:/module/action/var1/value1/var2/value2

你可以试试这样的事情:

# turn everything after /module/action/XXXX to query string variables
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([^/]+)/([^/]+)(.*)$ /$1/$2/$5?$3=$4 [L,QSA]
# turn /module/action/ into option= and action=
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?option=$1&action=$2 [L,QSA]
# turn /module/ into option= and action=main
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?option=$1&action=main [L,QSA]