【问题标题】:htaccess rewrite for codeigniterhtaccess 重写 codeigniter
【发布时间】:2012-09-10 09:26:00
【问题描述】:

我想重写codeigniter url rewrite的代码。

我使用了以下代码。

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /test/index.php?/$1 [L]
RewriteRue ^list/shows/cid-([0-9]+).html /list/shows?cid=$1

注意:“test”是我的网站目录。

它在删除 index.php 时效果很好。但它不适用于url重写

我要http://example.com/test/list/shows?cid=1

成为

http://example.com/test/list/shows/cid-1.html

提前致谢。

【问题讨论】:

  • 有错字吗? RewriteRule。提示:改为在最后一行添加[L]
  • 去掉[L]也没用
  • 您是否考虑从index.php-removal 规则中删除??我认为它的解析方式存在冲突-即(有效地)有两个问号。

标签: .htaccess codeigniter mod-rewrite rewrite


【解决方案1】:

为什么不使用框架内置的routing capabilities

您可以轻松地将这样的内容添加到application/config/routes.php

$route['list/shows/cid-([0-9]+)'] = '/list/shows/$1';

我省略了.html 部分,因为如果您将其设置为 URL 后缀,则不会考虑这一点

编辑:根据 OP 的第一条评论更新(在答案中移动了我的评论)

现在,所有的请求都被这个指令捕获:

RewriteRule ^(.*)$ /test/index.php?/$1 [L]

尝试在 RewriteEngine On 指令之后移动您的自定义规则,更正您的 RewriteRule 错字并按照 diEcho 的建议在规则中添加 [L]

【讨论】:

  • 其实url会有很多参数,例如:example.com/test/list/shows?cid=1&aid=2&pid=3 ……,你知道,ci路由不支持查询字符串,所以我必须通过apache重写规则..
  • 我明白了......你的例子有点误导。尝试在 RewriteEngine On 指令之后移动您的自定义规则,更正您的 RewriteRule 错字并在您的规则中添加 [L],正如 diEcho 建议的那样(现在,所有请求都由 RewriteRule ^(.*)$ /test/index.php?/$1 [L] 指令捕获)。跨度>
【解决方案2】:

试试

RewriteEngine on
RewriteBase /test
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /test/index.php/$1 [NC,L]
RewriteRule ^list/shows/cid-([0-9]+).html /list/shows?cid=$1 [NC]

【讨论】:

  • 还是不行,我找到了同样的问题,stackoverflow.com/questions/9153022/…我修改了重写规则的最后一行:RewriteRule ^/list/shows/cid-([0-9]+) .html index.php/list/shows?cid=$1,但也提示 404 not found。
  • 删除第一行ReWriteRule后尝试
  • 也许从第一条规则中删除[L]
  • @mic 我之前建议过,但用户说它没有帮助。
  • to ALL,即使我删除了 RewriteRule ^(.*)$ /test/index.php/$1 [NC,L],它仍然不起作用,有人在 CI 中成功吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-03
  • 1970-01-01
相关资源
最近更新 更多