Got 404 error on indexing in trying to do Search Engine Friendly URLs

Viewed 41

I am trying to switch my website url to be "SEO Friendly" and i found a simple idea by php: so move every not found page to index.php by .htaccess:

ErrorDocument 404 /index.php

and from there i have in index.php:

   $url = explode('/', urldecode($_SERVER['REQUEST_URI']));

    
if (empty($url[1])){
    include "home.php";
    die();
}  else if($url[1] == "p"){
    include "s/xp.php";
    die();
} else if ( $url[1] == "d"){
    include "s/xd.php";
    die();
} else if ( $url[1] == "c"){
    include "s/xc.php";
    die();
} else {
    http_response_code(404);
    include('s/error.php');
    die();
}

However, the server find this as 404, so i found a solution to add:

header("HTTP/1.1 200 OK");

now the pages open it works fine, nothing wrong! but when it comes to indexing! google find the pages are 404! even if they works fine!

is there a way to let the server believes its not error? or better solution?

2 Answers
ErrorDocument 404 /index.php

This is really a hacky way to implement a front-controller pattern. Apache will set a 404 HTTP response code that you then must override in your script.

Apache provides a directive just for this:

FallbackResource /index.php

The behaviour is similar... any request that would ordinarily return a 404 is sent to /index.php except a 200 OK response code is set. You must then set the 404 response in your code (which it looks like you are doing).

Reference:

i don't understand why/how, but this works:

<?php header("Status: 200 OK"); ?>
Related