apache - 根据页面名称重写 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=home和https://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;
}
}
?>```
解决方案
假设 index.php 是索引文件,下面的代码将允许 apache 捕获漂亮的 url 并调用正确的 php
RewriteEngine On
# any word without slash
RewriteRule ^([^/]*)$ index.php?page=$1 [L]
但是您需要更改代码以使用这些漂亮的 url
推荐阅读
- python-3.x - 如何避免在 wxPython 中创建相同的窗口?
- c# - 无法访问通过用户控件动态加载的控件(如文本框、复选框、复选框列表)中的数据
- bots - 使用 Dialogflow 代理从 Messenger Webview 检索回发
- django - 将工具提示数据切换添加到 django 中的表单
- javascript - RangeSlider 显示多个输出
- java - 使用 Mockito 测试 @Postconstruct
- swisscomdev - 发生错误时如何使用 CF CLI 删除应用程序
- tensorflow - TensorFlow 和遮罩(标记)图像
- java - Java 程序打印字符串中仅出现一次的字符
- uwp - 如何更改 UWP NavigationView 中 PaneToggleButton 的颜色?