【问题标题】:Codeigniter links broken in localhost本地主机中的 Codeigniter 链接断开
【发布时间】:2013-02-22 15:26:44
【问题描述】:

我正在为一家公司更新一个项目。我将文件传输到我的本地主机。所以项目在“localhost/project/”中。当我在浏览器中打开网站时,所有链接都指向“localhost/filename”,目录从项目文件夹向上一级。我做了一些研究,发现有人建议更改 htaccess,但仍然无法得到正确的结果。和建议请让我知道。如果你想要我的任何东西,请问。谢谢!

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

开始:

http://localhost/projectfolder/

<a href="/" title="">Home</a>

导致:

http://localhost/

配置.php:

$config['base_url'] = 'http://localhost/projectfolder/';
$config['index_page'] = "";
$config['uri_protocol'] = "AUTO";
$config['url_suffix'] = ".html";
$config['language'] = "english";
$config['charset'] = "UTF-8";
$config['enable_hooks'] = FALSE;
$config['subclass_prefix'] = 'MY_';
$config['permitted_uri_chars'] = 'a-z & + 0-9~%.:_\-,\'?';
$config['enable_query_strings'] = FALSE;
$config['controller_trigger']   = 'c';
$config['function_trigger']     = 'm';
$config['log_threshold'] = 0;
$config['log_path'] = '';
$config['log_date_format'] = 'Y-m-d H:i:s';
$config['cache_path'] = '';
$config['encryption_key'] = "";
$config['sess_cookie_name']     = 'ci_session';
$config['sess_expiration']      = 7200;
$config['sess_encrypt_cookie']  = FALSE;
$config['sess_use_database']    = FALSE;
$config['sess_table_name']      = 'ci_sessions';
$config['sess_match_ip']        = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update']  = 300;
$config['cookie_prefix']    = "";
$config['cookie_domain']    = "";
$config['cookie_path']      = "/";
$config['global_xss_filtering'] = FALSE;
$config['compress_output'] = FALSE;
$config['time_reference'] = 'local';
$config['rewrite_short_tags'] = FALSE;
$config['proxy_ips'] = '';

【问题讨论】:

标签: php codeigniter hyperlink broken-links


【解决方案1】:

您可能需要打开 config.php 文件并将 base_url 值更改为 localhost 或可能 localhost:8080(或您的 localhost 使用的任何端口)。

编辑

问题是您的所有锚标记都使用硬编码值,例如主页的/

在服务器上这可以正常工作,因为您的域类似于example.com,所以转到/,您就是说转到example.com/

在 codeigniter 中创建链接的正确方法是使用 anchor() 帮助程序,或者像您的项目一样手动创建锚标记,但在 href 中使用 base_url()site_url()

如何修复主页链接的示例如下:

<a href="<?php echo base_url(); ?>/">blah</a>

它可能适用于您的所有链接,只需在您的 href 值前面加上 base_url,但这取决于链接中的值。

这里是 codeigniter 辅助方法的链接 - http://ellislab.com/codeigniter/user-guide/helpers/url_helper.html

最好看一下 codeigniter 手册,因为它非常好。

【讨论】:

  • 那个文件在哪里?
  • /application/config/config.php
  • 好的,那么什么 url 会在浏览器中打开您的应用程序,当您单击不起作用的链接时会得到什么 url?
  • 另外,您使用的是 mamp、wamp 还是其他东西?
  • localhost/projectname 调出首页 ||使用 WAMP ||当我点击“主页链接”时,它会将我带到 localhost;wamp 主页
【解决方案2】:

更改以下行

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

to

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

希望它会起作用。

【讨论】:

    【解决方案3】:

    在配置文件中使用:

    $config['base_url'] = 'http://'.$_SERVER['SERVER_NAME'].'/Your project folder  name';
    $config['index_page'] = '';
    $config['uri_protocol'] = 'AUTO';
    

    在 .htaccess 中使用它

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

    并使用以下命令启用重写模式

    a2enmod rewrite
    

    同时编辑文件 /etc/apache2/sites-enabled/000-default

    change all the AllowOverride None to AllowOverride All. 
    

    【讨论】:

      【解决方案4】:

      试试这个简单的 .htaccess 它似乎对我有用:你的缺少 ReWriteBase


      <IfModule mod_rewrite.c>
      
          RewriteEngine on
          RewriteBase /projectfolder/
      
          # If the start of the URL doesn't match one of these...
          RewriteCond $1 !^(index\.php|robots\.txt|uploads|css|images|js)
      
          # Route through index.php
          RewriteRule ^(.*)$ index.php/$1 [L]
      
      </IfModule>
      

      如果这对你有用,我想你可以从修复你的 ReWriteBase 开始。

      请注意,当您将此 htaccess 文件上传到您的主机并且默认 base_url 更改为 www.example.com 之类的内容时,您必须将 ReWriteBase 更改为 /

      并且不要忘记在您的链接中尽可能使用&lt;?php echo base_url() ?&gt;

      希望对你有帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-07-09
        • 2012-08-23
        • 1970-01-01
        • 2013-07-28
        • 1970-01-01
        • 2015-10-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多