dsbo44836129 2019-07-06 17:04
浏览 55

如何通过正确的控制器响应修复Codeigniter 404头响应状态?

I have a codeigniter project that return only in some case 404 header response, but with controller response.

Problem affect SEO score and page not load on Microsoft browsers.

Its Codeigniter 3.0.3 on cloudways server instance with apache PHP 5.6. I try fix changed htaccess rules (i am newbie with that), and force header status 200 before execute load view code, but header status not change.

CODE INFORMATION

This its .htaccess::

<IfModule mod_rewrite.so>
   RewriteEngine On

   RewriteCond $1 !^(index\.php|images|stylesheets|javascript)
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteCond %{REQUEST_FILENAME} !-l
   RewriteRule ^(.*) /index.php/$1 [L]
   RewriteRule ^uploadify/.*$ - [PT]
   RewriteRule ^uploads/.*$ - [PT]

</IfModule>

<IfModule !mod_rewrite.so>
      ErrorDocument 404 /index.php
</IfModule>

RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

routes.php fragment:

$route['default_controller']        = 'apply'; // working
$route['apply-mortgage']            = 'apply/mortgage';  // not working

Same functions (index and mortgage) return a respective views with same code:

 $this->load->view();

I include MY_CONTROLLER too:

class MY_Controller extends CI_Controller {
  public function _remap($method, $params = [])
  {
    if (method_exists($this, $method)) {
      $response = $this->$method(...$params);
      if ($response === null) {
        $response = Response::LAST();
      }
      if ($response instanceof stdClass || in_array(gettype($response), ['array', 'boolean', 'double', 'integer'])) {
        $response = Response::json($response);
      } elseif (is_string($response)) {
        $response = Response::html($response);
      }
      if ($response instanceof Response) {
        $response->output();
        exit;
      }
      if (isset($response)) return $response;
    }
    show_404();
  }
}

BROWSER RESPONSE

Actual response on browser console header (network tab)

:status: 404
Content-Type: text/html; charset=UTF-8
Pragma: no-cache
. . .

preview (network tab) return view response on controller function:

<!doctype html>
<html lang="en">
<head>
...

I need that header response status return 200

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
    • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
    • ¥15 lingo18勾选global solver求解使用的算法
    • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
    • ¥15 Python3.5 相关代码写作
    • ¥20 测距传感器数据手册i2c
    • ¥15 RPA正常跑,cmd输入cookies跑不出来
    • ¥15 求帮我调试一下freefem代码
    • ¥15 matlab代码解决,怎么运行
    • ¥15 R语言Rstudio突然无法启动