【问题标题】:How to create several URL aliases for a single page?如何为单个页面创建多个 URL 别名?
【发布时间】:2017-07-29 13:18:20
【问题描述】:

我正在使用 PHP,并且我已经阅读了有关如何使用 create aliases 并在 Apache 中执行 rewrite 规则的信息。

问题是我更想要一个类似于我在 Drupal 站点中拥有的功能。

例如:

URL example.com/info.php?id=18&type=article 应该可以通过以下方式访问:

案例1: example.com/article/18

案例 2: example.com/special-url-to-the-article

我确实了解如何为每篇文章创建特定别名的基础知识(案例 2):

我认为我应该使用 $_GET['id']$_GET['type'] 之类的东西检查 url,以便在包含别名和其他值的数据库中查看它,如果别名存在,则形成一个新的 url id 和类型并将用户发送到那里。

但是如何管理案例 1,我在其中以编程方式设置内容类型和内容 id

我只需要一些可以为我指明方向的指导。

【问题讨论】:

    标签: php apache .htaccess url mod-rewrite


    【解决方案1】:

    我使用类似的重写,无论对错,这基本上就是我所做的。在这两种情况下,您的 $_SERVER 数组都应该具有以下一个或多个值:

    [REQUEST_URI] => /article/18/
    [QUERY_STRING] => article/18/
    [REDIRECT_URL] => /article/18/
    [REDIRECT_QUERY_STRING] => article/18/
    [SCRIPT_URL] => /article/18/
    [REDIRECT_SCRIPT_URL] => /article/18/
    

    对我来说,我会将故意页面的路径(如“关于我们”页面或其他任何内容)保存在数据库中,以便能够使用该路径查找它,但如果不存在,就像在这个情况,那么你进一步处理路径:

    # Assign one of the server paths that is most reliable
    $path  = $_SERVER['REDIRECT_URL'];
    # Check if there is a path that matches a database path
    $valid = $this->query("SELECT COUNT(*) AS count FROM `pages` WHERE `path` = ?",array($path))->getResults();
    # If there is a page that is in the database, show that one
    # Presumably this is what you mean by content and id programmically, ie.
    # it's a pre-designated page, not an article that needs to be broken down
    # but follows the same path pattern?
    if($valid['count'] == 1) {
        # Go about doing normal stuff
        # Stop by using die(), exit, or if in function/method, return
        # Idea is to stop at this stage of the script if successful
    }
    else {
        # Explode the query
        $parts = array_filter(explode('/',$path));
        # Check if both parts exist
        if(isset($parts[0]) && isset($parts[1])) {
            # Assign type
            $type = $parts[0];
            # Assign ID
            $id   = $parts[1];
            # Fetch the results (you'll want to sanitize or match from an array the $type value to avoid injection)
            $page = $this->query("SELECT * FROM `{$type}` WHERE `ID` = ?",array($id))->getResults();
            # Check if page is valid and display
            # Stop by using die(), exit, or if in function/method, return
        }      
    }
    
    # Send a 404 header and display a not found
    # Make the last thing happen show a 404 page by default  
    

    无论如何,这基本上就是我所做的,但您可能会进行重写以处理htaccess 文件中的两种情况。我个人只拥有一个汇集所有内容的工具,我使用 PHP 处理路径和路由,然后如果需要不同的场景,我不必创建一些用户控件来编辑 htaccess 文件。

    【讨论】:

    • 正如您在上一段中所建议的那样 - 此代码确实需要在您的 .htaccess 文件中使用 前端控制器(即对您的 PHP 脚本进行内部重写)。 $_SERVER['REDIRECT_URI']; - 这应该是REDIRECT_URL(不是_URI)。这不仅仅是“最可靠”的案例。这些变量确实包含不同的信息。例如,REQUEST_URI 包含完整的 URL,包括查询字符串,而 REDIRECT_URL 仅包含 URL 路径(并且仅在内部重写后设置)。
    • @MrWhite 是的,对不起,我在回答时粘贴了错误的键名,应该是 REDIRECT_URL(或列出的其他提供一致重定向路径的一个)。
    【解决方案2】:

    .htaccess中添加这些行

    第一种情况:

    RewriteRule ^article/([0-9]+)$ info.php?id=$1
    

    第二种情况:

    RewriteRule your-desire-url$ yourpage.php
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-20
      • 2011-02-25
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多