php 如何修复404错误时使用的SEO URL与htaccess重写规则

dgiusagp  于 2023-09-29  发布在  PHP
关注(0)|答案(2)|浏览(86)

我有在根目录/pages页页像object.php
在这些页面上,我通过GET获取seo_url,在object.php页面的脚本中,我搜索这个seo_url,并通过数据库查询获取它的ID
问题是,当你进入mysite.com/object/car/时,一个404页面被重定向。为什么会发生这种情况,以及如何在我的情况下正确地制作SEO URL?
我的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]

我的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"; 
}

在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)

请帮帮我当我按照链接mysite.com/object?seo_url=car一切正常,当我按照链接mysite.com/object/car/-错误404

xmd2e60i

xmd2e60i1#

我有提供代码只是检查它。

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"; 
}

成功后你已经删除echo,print_r. local system screen short

vptzau2j

vptzau2j2#

嗨,我认为第一步是,你必须检查.htacess工作或不使用此链接检查.htaccess启用ot不https://askubuntu.com/questions/421233/enabling-htaccess-file-to-rewrite-path-not-working
htaccess checking

相关问题