Rewriting URLs in PHP instead of server config

前端 未结 3 1510
南旧
南旧 2021-01-28 23:35

I\'m looking for a very lightweight routing framework (to go with php-skel).

The first thing I\'d like to investigate is specifying rewrite rules in a php file (\"not fo

3条回答
  •  再見小時候
    2021-01-28 23:44

    PHP can't rewrite URLs the way mod_rewrite can, it can only redirect to other pages, which'd basically double the number of hits on your server (1 hit on php script, 2nd hit on redirect target).

    However, you can have the PHP script dynamically load up the "redirect" pages' contents:

    switch($_GET['page']) {
        case 1:
            include('page1.php');
            break;
        case 2:
            include('page2.php');
            break;
        default:
            include('page1.php');
    }
    

    this'd be pretty much transparent to the user, and you get basically the same effect as mod_rewrite. With appropriate query and path_info parameters, you can duplicate a mod_write "pretty" url fairly well.

提交回复
热议问题