【问题标题】:CodeIgniter failed when the url does not end with the suffix .html当 url 不以后缀 .html 结尾时,CodeIgniter 失败
【发布时间】:2015-04-12 05:02:26
【问题描述】:

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

我的 config.php 中的 url_config 是这样的:

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

当我访问 URL 时,例如。

http://localhost/MyProjects/CodeIgniter/welcome/index

http://localhost/MyProjects/CodeIgniter/welcome/index.html

页面已加载。

我的问题是,当url不以后缀.html结尾时,如何设置CodeIgniter失败?
所以,当我这样输入时

http://localhost/MyProjects/CodeIgniter/welcome/index

请求将失败或重定向到禁止

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    尝试使用它来重定向它:

    $this->load->helper('url'); 
    if (preg_match("/.html/i", current_url())) {
        echo "A match was found.";
    } else {
        // redirect(base_url());
        show_404(); //if you want to show page not found
    }
    

    function index() 放入welcome.php 控制器中。

    【讨论】:

      【解决方案2】:

      感谢您的解决方案,但我会像这样对您的解决方案进行一些修改

      if (preg_match("/.html/i", $_SERVER['REQUEST_URI'])) {
          echo "A match was found.";
      } else {
          // redirect(base_url());
          show_404(); //if you want to show page not found
      }
      

      因为如果我使用 current_url() 函数,那么 CodeIgniter 总是提供在 $config['url_suffix']='.html'; 中设置的 .html 扩展名 所以我使用$_SERVER['REQUEST_URI'],因为它提供了地址栏中实际存在的内容。

      感谢您的建议,我的问题已经解决了。如果你来自印度尼西亚,makasih gan

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-10-11
        • 2014-07-06
        • 2010-10-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-04
        相关资源
        最近更新 更多