【问题标题】:CodeIgniter .htaccess and base_url() issuesCodeIgniter .htaccess 和 base_url() 问题
【发布时间】:2012-06-25 13:00:52
【问题描述】:

我最近开始研究 CodeIgniter,我遇到了一些与 .htaccess 和 base_url() 函数有关的问题,这会导致问题。我希望这只是一个菜鸟错误,并且有一个快速修复!

几乎发生的事情是,现在有了 .htaccess,index.php 就从 url 中消失了,这太棒了,但现在我在将样式表和 js 文件与我的模板链接时遇到了问题。

在我阅读/观看的所有教程中,它们都包含了 .htaccess,然后仍然设置它们的 base_url(),然后使用“link_tag(href);”链接他们的 .css 但现在我在 url 上加倍了。例如,当我尝试添加样式表时,它查找的路径是:

http://www.mysite.com/site/www.mysite.com/site/css/stylesheet.css

所以我的第一个想法就是删除我的 base_url() 以便 .htaccess 完成所有操作,但它不起作用。使用 .htaccess,我可以拥有一个没有 index.php 的站点,但没有链接样式表或 JavaScript 文件,或者我可以拥有一个具有一些样式的站点,但处理 index.php。一定有一种方法可以同时工作吗?

另外,我没有使用 link_tag() 进行了修复,我只是回显了一个正常的链接标签,该标签在开始时仍然可以正常工作:

http://www.mysite.com/

但是如果登录不正确并且我重定向到再次加载登录视图,样式就会消失,因为它现在使用的相对路径来自:

http://www.mysite.com/user/login

提前致谢!任何帮助将不胜感激。

【问题讨论】:

    标签: php .htaccess codeigniter base-url


    【解决方案1】:

    试试

    RewriteEngine on
    RewriteCond $1 !^(css|js)
    RewriteRule ^(.*)$ /index.php/$1 [L]
    

    所以www.mysite.com/css/mycss.css 被正常检索,而www.mysite.com/something 被传递给codeigniter。

    要获得带有base_url() 的css 文件,您可以使用base_url("css/mycss.css"),这将导致"http://www.mysite.com/css/mycss.css"

    【讨论】:

    • @nickcorin 如果这对您有用,请考虑投票并接受答案。谢谢。
    【解决方案2】:

    更好的 .htaccess 规则集是:

    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]
    
    #When your application folder isn't in the system folder
    #This snippet prevents user access to the application folder
    #Submitted by: Fabdrol
    #Rename 'application' to your applications folder name.
    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]
    
    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
    

    这不允许访问您的系统和应用程序文件夹,但允许访问所有其他文件,例如:/js/*、/css/*、/img/* 等。

    【讨论】:

    • 你可以简单地将php文件完全放在其他地方,这个目录只需要index.php(和css/js等静态目录)
    猜你喜欢
    • 1970-01-01
    • 2014-06-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-09
    • 2011-10-04
    • 1970-01-01
    • 2017-02-09
    • 2013-01-24
    相关资源
    最近更新 更多