【问题标题】:urlrewrite images to codeigniter controller method gives page not found errorurlrewrite 图像到 codeigniter 控制器方法给出页面未找到错误
【发布时间】:2020-01-30 11:30:26
【问题描述】:

我有一个 codeigniter 站点启动并运行,没有任何问题。现在我需要将一些图像重写为 codeigniter 控制器/方法。

这是我的 .htaccess 文件,可以正常工作

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt|css|docs|js|media|static|public)
RewriteRule ^(.*)$ /index.php?/$1 [L]

现在我需要这样的东西来将图像重写为 codeigniter。 重写而不是重定向

RewriteRule ^(media/somefolder/someimage.jpg)$ /some_controller/somemethod?param=value [L]

我添加了以下行(这不是 codeigniter 只是一个常规的 php 文件)只是为了测试以确保 RewriteRule 工作并且它工作正常

RewriteRule ^(media/somefolder/someimage.jpg)$ public/myphpfile.php?ipath=$1 [L]

但如果我尝试以下任何操作,我会得到 codeigniter's page not found 错误页面。

RewriteRule ^(media/somefolder/someimage.jpg)$ /some_controller/somemethod/?ipath=$1 [L]

RewriteRule ^(media/somefolder/someimage.jpg)$ index.php?c=some_controller&m=somemethod&ipath=$1 [L]

我在这里错过了什么?

【问题讨论】:

  • 我可以知道您希望ipath 包含什么值吗?
  • 括号中的整个匹配网址

标签: codeigniter mod-rewrite url-rewriting codeigniter-3


【解决方案1】:

仅对特定图像添加例外

像这样添加media 规则:

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteRule ^(media/somefolder/someimage.jpg)$ /index.php?/some_controller/somemethod/?ipath=$1 [L]
RewriteCond $1 !^(index\.php|images|robots\.txt|css|docs|js|media|static|public)
RewriteRule ^(.*)$ /index.php?/$1 [L]

为媒体目录中的所有图像添加例外

由于您已过滤掉以忽略此规则中的 media 目录:

RewriteCond $1 !^(index\.php|images|robots\.txt|css|docs|js|media|static|public)

为了将media 路径重写到控制器而不是忽略它,请从中删除media 规则:

RewriteCond $1 !^(index\.php|images|robots\.txt|css|docs|js|static|public)

像这样添加media 规则:

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
# removed the media form below rule
RewriteCond $1 !^(index\.php|images|robots\.txt|css|docs|js|static|public)
RewriteRule ^(media/somefolder/someimage.jpg)$ /index.php?/some_controller/somemethod/?ipath=$1 [L]
RewriteRule ^(.*)$ /index.php?/$1 [L]

【讨论】:

  • 恐怕这会给我 500 个内部服务器错误。请求超出了 10 个内部重定向的限制。
猜你喜欢
  • 2013-10-04
  • 2019-04-22
  • 2015-10-09
  • 2015-03-30
  • 1970-01-01
  • 1970-01-01
  • 2021-10-29
  • 2013-07-20
  • 1970-01-01
相关资源
最近更新 更多