Welcome Guest, Not a member yet? Register   Sign In
Help, How to fix Codeigniter 404 response status with content on specific case
#1

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:
```php
$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:
```php
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)
```
Confusedtatus: 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
Reply
#2

You got show_404() in your controller. I should fix why it's returning 404 in the first place and it's incorrect to force status 200 for something that should be 404.
Reply
#3

(07-11-2019, 11:37 PM)jreklund Wrote: You got show_404() in your controller. I should fix why it's returning 404 in the first place and it's incorrect to force status 200 for something that should be 404.

I remove 404 but not working. 

Problem its that controller->method exist. Not logic that response return correctly but header include 404.
Reply
#4

Did you look at the server log files to see what is going on?
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

These are server logs. (mmm Codeigniter nog generate anything with "log_threshold" on 4)

==> php-app.access.log <==
SOMEIP, 127.0.0.1 - [29/Jul/2019:13:03:59 +0000] "GET /index.php" 200 0 - 1169 8494 0.025 1835008 40.79% 0.00% "/apply-business-loans"

==> apache_phpstack-38010-80732-222717.cloudwaysapps.com.access.log <==
SOMEIP - - [29/Jul/2019:13:03:59 +0000] "GET /apply-business-loans HTTP/1.0" 404 38858 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1.1 Safari/605.1.15"

==> nginx-app.status.log <==
status_code:404 SOMEIP [29/Jul/2019:13:03:59 +0000] GET /apply-business-loans HTTP/1.1
Reply
#6

log_message($level, $message)

Read the note at the bottom.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#7

Log print all.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB