1.首先,在.htaccess(位置在网站根目录)文件里设置(作用是隐藏index.php),如下:
2 | RewriteCond $1 !^(index\.php|images|js|img|css|robots\.txt) #在这里写要排除的资源等 |
3 | RewriteRule ^(.*)$ index.php/$1 [L] |
2.把JS,CSS,IMG等资源文件夹与SYSTEM文件夹放在同一级下,然后在JS文件夹中建立ajax.js文件,在VIEW层中的文件index.html要引入JS时,如下:
1 | <script type="text/javascript" src="网站绝对URL/js/ajax.js'?>"></script>:注网站绝对URL即 http://localhost/ |
3.在application\config\config.php文件中设置base_url :
1 | $config['base_url'] =http://blog.woodbunny.com/blog; //这里是你的网站根目录 |
4.在contoler构造方法中调用如下代码:
1 | $this->load->helper('url') |
5.在VIEW层中具体页面中引入即可:
1 | <script type="text/javascript" src='<?=base_url().'js/Ajax.js'?>'></script> |
6.可以使用html的<base href=''<?=base_url()?>"来简化:
1 | <script type="text/javascript" src='js/Ajax.js'></script> 这一点在导入图片时非常有用. |
注:“RewriteCond $1 !^(index\.php|images|js|img|css|robots\.txt) ”的意思是:任意你想访问的资源都不被重定向时,都可写在这里。有时,网站没有加载CSS,JS(它的路径都是正确的)时,都是被重定向了,这要注意.