dqdjfb2325 2013-08-12 11:31
浏览 74
已采纳

使用mod_rewrite模式匹配errorDocument规则

Having a little difficulty implementing error handling for 404 to my site. Normally this is not an issue but in this case the set up is different.

I am having to use pattern matches in my .htaccess file for URL rewrites i.e. RewriteRule ^([^/]+)\/$ and RewriteRule ^products\/([^/]+)\/([^/]+)\/$ this is due to the site having a CMS controlled by other users who can add new pages I.e. www.yourdomain.com/pagename "pagename" being any new page.

Any URL typed in lets say "/about" will match RewriteRule ^([^/]+)\/$ then be checked in a database to find a match, if a match is found then the user is redirected to the correct page, if no match then I could redirect the user to a specific page or set a http status code.

Only problem with that is I would want to preserve the incorrect URL and a PHP redirect would overwite that.

code:

ErrorDocument 403           /errors.php?error=403
ErrorDocument 404           /errors.php?error=404
ErrorDocument 500           /errors.php?error=500
ErrorDocument 503           /errors.php?error=503

RewriteEngine                                  On

RewriteRule ^([^/]+)\/$                     ?cat=generic&page=$1 [L]
RewriteRule ^products\/([^/]+)\/([^/]+)\/$  ?cat=product&page=$2 [L]

Now I am pretty sure the issue is due to having the rewrite rules I have in place, so what I suppose I am looking for is an alternative to the rewrites that still allow me to grab any newly created page url but also allows for successful error document handling.

If I have left out any vital code/information please let me know and I shall edit the post with it.

Thank you in advance for any advice/help.

EDIT::

PHP script that handles the page content.

function page_handler() {
    if(!isset($_GET['cat'])):
        $_GET['cat'] = 'generic';
        $_GET['page'] = 'home';
    endif;

    // Connect to the database
    $link = db_connect_cms();

    // Grab the id of the page
    $qry = mysqli_query($link,"
        SELECT hash,slug
        FROM cms_web_pages
        WHERE slug='".$_GET['page']."'
        AND published=1")

        or die(mysqli_error($link)
    );

    $row = mysqli_fetch_object($qry);

    // If there is a match in the db
    if(mysqli_num_rows($qry) > 0):
        $id_hash = $row->hash;
    else:
        $_GET["error"] = "404";
        require_once($_SERVER["DOCUMENT_ROOT"] . "/errors.php");
    endif;

    return array($id_hash);
}
  • 写回答

2条回答 默认 最新

  • duanlu7223 2013-08-12 12:16
    关注

    You can achieve that by replacing this line in your PHP code:

    header("location: /errors.php?error=404");
    

    with this:

    // have this code when DB lookup fails
    $_GET["error"] = "404";
    include($_SERVER["DOCUMENT_ROOT"] . "/errors.php");
    exit; // required to prevent showing current page after error document
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.