【问题标题】:How to modify the URI routing for a MVC PHP site?如何修改 MVC PHP 站点的 URI 路由?
【发布时间】:2011-06-04 05:30:51
【问题描述】:

我正在尝试重写一个我使用 PHP/Apache 构建的 ASP.NET MVC 2 站点,以获取乐趣。

我已经构建了一个基本的 MVC 框架来处理请求。请求通过 index.php?controller=&action=&otherstuff=123123

执行

我希望能够通过输入 /controller/action?otherstuff=123123 来浏览我的网站

网络主机支持 mod_rewrite,我可以使用与 index.php 脚本位于同一位置的 .htaccess 文件进行设置吗?我如何让它发挥作用?

【问题讨论】:

    标签: php apache model-view-controller mod-rewrite uri


    【解决方案1】:

    试试这个:

    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^/([^/]+)/([^/]+)?otherstuff=(.*)^ index.php?controller=$1&action=$2&otherstuff=$3 [L]
    

    【讨论】:

      【解决方案2】:

      我想我建议您使用Query String Append / qsa 指令。这意味着如果没有传递查询字符串,您的重写将继续正常工作。

      RewriteEngine On
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^/([^/]+)/([^/]+)$ index.php?controller=$1&action=$2 [qsa,last]
      

      这样,如果您决定稍后使用更多查询字符串,请继续 - 它会通过它们。

      G

      【讨论】:

      • 谢谢。我确实对您发布的规则进行了小幅调整。 RewriteRule ^(.[^/]*)/(.[^/]*)[/]?$ /index.php?controller=$1&action=$2 [L,QSA]
      【解决方案3】:

      这就是我正在使用的。框架解析路由。

      <IfModule mod_rewrite.c>
          RewriteEngine On
      
          RewriteCond %{REQUEST_FILENAME} !-f
          RewriteCond %{REQUEST_FILENAME} !-d
          RewriteCond %{REQUEST_URI} !=/favicon.ico
          RewriteRule .* index.php/$0 [PT]
      </IfModule>
      

      【讨论】:

        猜你喜欢
        • 2018-04-21
        • 2010-10-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多