我正在使用PHP,并且我已阅读有关如何create aliases并在rewrite中执行Apache规则的内容。
问题是我宁愿喜欢类似于Drupal网站中的功能。
网址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']
的内容检查网址,以便在包含别名和其他值的数据库中查看它,如果存在别名,则一个带有id和type的新url并将用户发送到那里。
但是如何管理案例1,我以编程方式设置内容类型和内容id
?
我只需要一些指导,可以指出我的方向。
答案 0 :(得分:2)
我使用类似的重写,对或错,这基本上就是我做的。在这两种情况下,您的$_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
文件这条线。
答案 1 :(得分:2)
在.htaccess
第一案:
RewriteRule ^article/([0-9]+)$ info.php?id=$1
对于第二种情况:
RewriteRule your-desire-url$ yourpage.php