【问题标题】:Routing and SEFURL with .htaccess on an independent project在独立项目上使用 .htaccess 进行路由和 SEFURL
【发布时间】:2013-10-08 09:59:42
【问题描述】:

我开发了一个小型(功能强大且轻量级 :-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

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

【问题讨论】:

    标签: php regex apache .htaccess mod-rewrite


    【解决方案1】:

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

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

    就可以无限期使用的 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]
    

    【讨论】:

    • 它不工作大声笑。因此,如果您将 L 引号放在末尾,它将作为最后一条规则,并且不会继续分析。问题是......我该怎么做才能不断重写? .所以可以说我有以下字符串 var1/a/var2/b/var3/c/var4/d/var5/d/var6/f 程序将匹配第一个 var1/a/var2/a 然后留下其余的 / var3/c/var4/d.... 对于其他两个不匹配的规则:S
    猜你喜欢
    • 2013-12-08
    • 2016-05-01
    • 2016-01-09
    • 2012-07-08
    • 2019-06-08
    • 2011-11-23
    • 2019-01-07
    • 2014-11-21
    • 2019-03-05
    相关资源
    最近更新 更多