【问题标题】:Rewrite the URL based on page name根据页面名称重写 URL
【发布时间】:2021-10-27 03:00:40
【问题描述】:

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

客户端登录该站点后,它会重定向到https://app.bitlabtech.com/?page=home,当他们请求另一个页面(如发送邮件)时,URL 看起来像https://app.bitlabtech.com/?page=sendEmail。我想将 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;
    }   
}

?>```

【问题讨论】:

  • 上面的 PHP 代码无关紧要,您应该发布的是您的 Apache 配置,即 httpd.conf 或 apache2.conf 或 .htaccess,因为您将在 Apache 中使用 mod_rewrite 创建一个重写规则来实现你在找什么
  • PHP 代码用于显示我的 URL 是如何形成的,而不是用于重写。你能再澄清一点吗?
  • 您只需要说出您的网址是什么以及您希望它像您在第一段中所做的那样。这些信息足以在 apache 配置文件中创建重写规则。然后你应该发布你当前的 apache 配置是什么,以便人们可以向你展示如何集成 rerite 规则

标签: apache url url-rewriting


【解决方案1】:

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

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

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

【讨论】:

  • 这对我不起作用。请问我需要改什么?我尝试在我的 .htaccess RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] RewriteRule ^([^/]*)$ index.php?page=$1 [L] 中使用以下代码
猜你喜欢
  • 2023-03-25
  • 2011-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-19
  • 2021-06-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多