【问题标题】:htaccess match url by category namehtaccess 按类别名称匹配 url
【发布时间】:2015-04-08 17:37:58
【问题描述】:

我正在使用具有以下 url 实现的 php 中的自定义 MVC 模式

www.test.com/module/controller/action?params

通过 php,我将 url 拆分为查找模块、控制器、操作。效果很好。

我有情况显示像www.test.com/sale 这样的网址。以前我的网址是www.test.com?id=1,其中id 是类别sale,它将通过php 脚本调用相应的模块、控制器、操作。

我正在使用以下 htaccess

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

我的重写更改不应影响现有流程,当我点击 url www.test.com/sale 时,我需要专门应用重写条件,它应该调用 www.test.com?id=1,就像我可以用于任何其他类别一样。

【问题讨论】:

  • /sale 是唯一的类别还是您有更多?
  • @anubhava 我有不止一个类别。
  • 在这种情况下,重写规则无法通过查找某些数据库将类别转换为 id
  • @anubhava 只有 3 个类别,所以我只想在 htaccess 中自己做。有可能

标签: php regex .htaccess model-view-controller url-rewriting


【解决方案1】:

你可以这样做:

RewriteEngine on

RewriteRule ^sale/?$ index.php?id=1 [QSA,NC,L]
RewriteRule ^purchase/?$ index.php?id=2 [QSA,NC,L]
RewriteRule ^admin/?$ index.php?id=3 [QSA,NC,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

【讨论】:

  • 当我点击 localhost/test/sale 时,它会进入 wamp 索引页面。现有的 url 也进入 wamp 页面
  • 删除 rewriteBase / 后得到它 :) 谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-31
  • 2011-05-01
  • 2021-11-02
  • 2017-03-19
  • 1970-01-01
  • 2021-07-22
  • 1970-01-01
相关资源
最近更新 更多