首页 > 解决方案 > 根据页面名称重写 URL

问题描述

我有一个包含 URL https://app.bitlabtech.com/的站点,适用于普通用户和管理员用户https://app.bitlabtech.com/manager/

客户端登录该站点后,它会重定向到https://app.bitlabtech.com/?page=home,当他们请求另一个页面(如发送邮件)时,URL 看起来像https://app.bitlabtech.com/?页面=发送电子邮件。我想将 URL 重写为https://app.bitlabtech.com/home而不是https://app.bitlabtech.com/?page=homehttps://app.bitlabtech.com/sendEmail而不是https: //app.bitlabtech.com/?page=sendEmail

在 UBUNTU 20.04 apache 服务器上运行的代码。

为了更好地理解,我在这里附上了我的 PHP 代码

    public static $page = "page";
    public static $folder = PAGES_DIR;

    public static function getParam($param = null){
        if(!empty($param)) {
            return isset($_GET[$param]) && $_GET[$param] != "" ? $_GET[$param] : null;
        }
    }

    public static function cPage(){
        return isset($_GET[self::$page]) ? $_GET[self::$page] : "index";
    }

    public static function getPage(){
        $page = self::$folder.DS.self::cPage().".php";
        $error = self::$folder.DS."error.php";

        return is_file($page) ? $page : $error;
    }

    public static function getReferrerUrl() {
        $page = self::getParam(Login::$_referrer);
        return !empty($page) ? "/?page={$page}" : null;
    }   
}

?>```

标签: apacheurlurl-rewriting

解决方案


假设 index.php 是索引文件,下面的代码将允许 apache 捕获漂亮的 url 并调用正确的 php

RewriteEngine On
# any word without slash
RewriteRule ^([^/]*)$   index.php?page=$1 [L]    

但是您需要更改代码以使用这些漂亮的 url


推荐阅读