Search code examples
phpurlslug

How to fix 404 error when using SEO URLs with htaccess rewrite rule


I have pages in root directory/pages pages like object.php

On these pages I get seo_url via GET and in the script on the object.php page I search for this seo_url and get the id for it via a database query

the problem is that when you go to mysite.com/object/car/, a 404 page is redirected. Why does this happen and how to make SEO urls correctly in my case?

Thats my htaccess

RewriteEngine On
RewriteBase /

Options +FollowSymLinks
php_flag display_errors off
php_flag allow_url_fopen off
php_value error_reporting 0

# SSL
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

#catalogue
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^object/(.*)$ object.php?seo_url=$1 [L,QSA]

RewriteCond %{REQUEST_FILENAME} !^/index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url_param=$1 [L,QSA]

and thats my index.php

$url = $_SERVER['REQUEST_URI'];
$url = explode('?', $url)[0];
$pages = scandir($_SERVER['DOCUMENT_ROOT'] . '/pages'); 

$found = false;

foreach ($pages as $page) {  
    
    if ($page !== "." && $page !== "…") { 
        $page_name = pathinfo($page, PATHINFO_FILENAME); 
        if ($url === "/" . $page_name) { 
            include $_SERVER['DOCUMENT_ROOT'] . "/pages/" . $page; 
            $found = true; 
            break; 
        } 
    } 
}

if ($url === '/') { 
    include $_SERVER['DOCUMENT_ROOT'] . "/pages/main.php"; 
    $found = true; 
}

if (!$found) { 
    include $_SERVER['DOCUMENT_ROOT'] . "/pages/404.php"; 
} 

in object.php

$seo_url = $_GET["seo_url"];

$id = $objects->seo_url_object($seo_url);  (here is a sql query to search for id by seo_url)

Please help. When I follow the link mysite.com/object?seo_url=car everything works correctly, when I follow the link mysite.com/object/car/ - error 404


Solution

  • I have provide code just check it.

    echo $currentURL = $_SERVER['REQUEST_URI'];;
    
    // Split the URL by '/' into an array
    $urlParts = explode('/', $currentURL);
    echo "<pre>";
    print_r($urlParts);
    $requestPage = "";
    if(isset($urlParts[1]) && $urlParts[1] == "object")
    {
      $requestPage = $urlParts[2];
    }
    
    
    
    $pages = scandir($_SERVER['DOCUMENT_ROOT'] . '/pages'); 
    //echo "<pre>";
    $found = false;
    //print_r($pages);
    foreach ($pages as $page) {  
        //print_r($page);
        if ($page !== "." && $page !== "…") { 
            $page_name = pathinfo($page, PATHINFO_FILENAME); 
            print_r([$requestPage,$page_name]);
            if ($requestPage == $page_name) { 
              echo "found page";
                include $_SERVER['DOCUMENT_ROOT'] . "/pages/" . $page; 
                $found = true; 
                break; 
            } 
        } 
    }
    if ($url === '/') { 
        include $_SERVER['DOCUMENT_ROOT'] . "/pages/main.php"; 
        $found = true; 
    }
    
    if (!$found) { 
        include $_SERVER['DOCUMENT_ROOT'] . "/pages/404.php"; 
    } 
    

    after success you have remove echo, print_r. local system screen short