Php 如何为单个页面创建多个URL别名?

Php 如何为单个页面创建多个URL别名?,php,apache,.htaccess,url,mod-rewrite,Php,Apache,.htaccess,Url,Mod Rewrite,我正在使用PHP,我已经阅读了关于如何在中创建和执行规则的内容 问题是,我更喜欢类似于Drupal站点的功能 例如: URLexample.com/info.php?id=18&type=article应该可以通过以下方式访问: 案例1:example.com/article/18 及 案例2:example.com/文章的特殊url 我确实了解如何为每篇文章创建特定别名的基础知识(案例2): 我认为我应该用$\u GET['id']和$\u GET['type']之类的东西检查url,以便在保

我正在使用PHP,我已经阅读了关于如何在中创建和执行规则的内容

问题是,我更喜欢类似于Drupal站点的功能

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

案例1:
example.com/article/18

案例2:
example.com/文章的特殊url

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

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

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


我只需要一些指导,可以为我指出一些方向。

我使用类似的重写,不管是对是错,这基本上就是我所做的。在这两种情况下,
$\u服务器
数组应具有以下一个或多个值:

[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
文件。

我使用类似的重写,不管是对是错,这基本上就是我所做的。在这两种情况下,
$\u服务器
数组应具有以下一个或多个值:

[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

第1例:

RewriteRule ^article/([0-9]+)$ info.php?id=$1
第二种情况:

RewriteRule your-desire-url$ yourpage.php

将这些行添加到
.htaccess

第1例:

RewriteRule ^article/([0-9]+)$ info.php?id=$1
第二种情况:

RewriteRule your-desire-url$ yourpage.php

正如您在上一段中所建议的,此代码确实需要
.htaccess
文件中的前端控制器(即,对PHP脚本进行内部重写)<代码>$\u服务器['REDIRECT\u URI']-这可能是
重定向\u URL
(而不是
\u URI
)。这不仅仅是“最可靠”的例子。这些变量确实包含不同的信息<例如,code>REQUEST_URI包含完整的URL,包括查询字符串,而
REDIRECT_URL
仅包含URL路径(并且仅在内部重写后设置)。@MrWhite Yeah抱歉,我在回答时粘贴了错误的键名,应该是REDIRECT_URL(或列出的提供一致重定向路径的其他路径之一).正如您在上一段中所建议的-此代码确实需要在
.htaccess
文件中使用前端控制器(即,对PHP脚本进行内部重写)<代码>$\u服务器['REDIRECT\u URI']-这可能是
重定向\u URL
(而不是
\u URI
)。这不仅仅是“最可靠”的例子。这些变量确实包含不同的信息<例如,code>REQUEST_URI包含完整的URL,包括查询字符串,而
REDIRECT_URL
仅包含URL路径(并且仅在内部重写后设置)。@MrWhite Yeah抱歉,我在回答时粘贴了错误的键名,应该是REDIRECT_URL(或列出的提供一致重定向路径的其他路径之一).