【问题标题】:CodeIgniter URL suffix .html automatically for all URLCodeIgniter URL 后缀 .html 自动为所有 URL
【发布时间】:2014-07-06 18:22:45
【问题描述】:

我是 CodeIgniter 的新手。 我在我的本地主机中设置 CI。 localhost/MyProjects/CodeIgniter/welcome/index

我可以从 url 中删除 index.php 并且我还完成了 .html 后缀 所以,我可以访问 localhost/MyProjects/CodeIgniter/welcome/index.html 并且它工作正常。

所以我正在使用这样的标签:

<a href="<?php echo base_url(); ?>welcome/register.html">Register</a>

但是,我不想手动使用链接中的 .html。

有任何解决方案来自动显示我的访问者 .html 后缀。 localhost/MyProjects/CodeIgniter/welcome/index.html 或 localhost/MyProjects/CodeIgniter/welcome/about.html 等

这是我的 htaccess http://pastebin.com/cXUFjvbp

【问题讨论】:

    标签: php .htaccess codeigniter


    【解决方案1】:

    只需在 config.php 中编辑 url_suffix .....

    $config['url_suffix'] = '.html';
    

    使用site_url()

    <!-- No need .html -->
    <a href="<?php echo site_url('welcome/register'); ?>">Register</a>
    <!-- <a href="http://domain/welcome/register.html">Register</a> -->
    

    anchor()

    // No need .html
    echo anchor('welcome/register', 'Register');
    // <a href="http://domain/welcome/register.html">Register</a>
    

    注意:别忘了用$this-&gt;load-&gt;helper('url');加载URL helper

    【讨论】:

    • 你好朋友,我用了你的方法,现在效果很好。非常感谢。
    • CI 3 发布日期:2015 年 3 月 30 日。2014 年 5 月 19 日回答:)
    【解决方案2】:

    更改您的 config.php 文件

    $config['url_suffix'] = '.html';
    

    【讨论】:

      【解决方案3】:

      只需重新定义 base_url 函数或创建一个新函数。看base_urlsource方法

      function _base_url($path = NULL) {
        return base_url() . $path . '.html';
      }
      

      【讨论】:

      • 制作一个新的是更好的解决方案。或者函数_base_url($path = NULL, $suffix = "") {return base_url($path).$suffix}
      • 为什么需要新功能?
      • 感谢您的解决方案。 :) 但是,我使用了@WahyuKristianto 方法。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-29
      • 1970-01-01
      • 2013-04-27
      相关资源
      最近更新 更多