【问题标题】:CodeIgniter 3 : Rewriting URL with assetsCodeIgniter 3:用资产重写 URL
【发布时间】:2018-06-07 17:40:51
【问题描述】:

我在 CodeIgniter 3 中遇到了 URL 重写问题。

我的文件结构是:

site
    -assets (.css and .js files)
    -application (sites files : controller, ...)
    -system (CI files)
    -user_guide

为了避免 index.php/ 在 URL 中,我把这个配置:

.htaccess

RewriteEngine on
RewriteCond $1 !^(index.php|resources|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

config.php

$config['index_page'] = '';

这样,我的 URL 不是 test.com/index.php/user/function 而是 test.com/user/function . 实际上一切都很好,但是当一些 .js 脚本尝试使用相对 URL 调用其他脚本时,它会调用错误的 URL。 例如,如果我的脚本之一想要调用 assets/js/jsfile.js,我会遇到 404 错误,因为它调用了 http://www.test.com/user/assets/js/jsfile.js,而它应该是 http://www.test.com/assets/js/jsfile.js

为了避免这种情况,我在每个<link><script> 中都添加了<?php echo base_url() ?> 我给自己打电话,但是一些.js 库需要实习生调用,我不能每次都这样做......我想要真正的解决方案...任何想法?非常感谢!

【问题讨论】:

  • 你是否也在config.php中设置了你的base_url
  • 是的,我有!它运行良好,因为在我的
  • 既然你提到了`
  • 您好!是的,我有 !他们对像“assets/js/..”这样的相对 uri 有问题,我们将其转换为“test.com/user/assets...”,所以调用我的控制器类!我必须输入“href='才能使其正常工作!但是有些库调用亲戚 uro 仍然存在问题,我的配置正在系统地重定向到我的类控制器...

标签: php .htaccess codeigniter routing codeigniter-3


【解决方案1】:

像这样更改您的 htaccess 文件;

RewriteCond $1 !^(index.php|resources|robots.txt|assets)

【讨论】:

  • 谢谢!我试过你的解决方案,但它没有解决我的问题...... :'(
  • 嗨,John,试试这个:“RewriteCond $1 !^(index\.php|resources|robots\.txt|assets)”
猜你喜欢
  • 2014-12-31
  • 2018-04-20
  • 2013-08-28
  • 2012-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多