我正在使用我自己制作的 PHP CMS。它获取 $_GET 参数 url
并将其转换为 www.website.com/{url}
。此 CMS 使用 $_GET 参数来获取文件。因此,如果 url
是 index
,则 CMS 会搜索文件 index
并返回文件的内容。但现在,我正在扩展 CMS 以添加一个附加参数,例如 profile/{username}
。我该如何扩展它?我希望我的网址为 www.website.com/profile/{username}
。我该怎么做呢?这是我当前的 htaccess:
RewriteEngine On
RewriteRule ^(|/)$ index.php?url=$1
RewriteRule ^([a-zA-Z0-9_-]+)(|/)$ index.php?url=$1a
这是 TPL 系统。
public function addTpl() {
global $zip;
if(!isset($_GET['url']) || empty($_GET['url'])) {
$_GET['url'] = 'index';
}
if(file_exists('_zip/_templates/_front/'. $zip['Template']['Front'] . '/')) {
if(file_exists('_zip/_templates/_front/'. $zip['Template']['Front'] . '/' . secure($_GET['url']) . '.php')) {
ob_start();
include('_zip/_templates/_front/'. $zip['Template']['Front'] . '/' . secure($_GET['url']) . '.php');
$this->tpl .= ob_get_contents();
ob_end_clean();
} else {
die(zipError('File Not Found', 'The file <b>' . secure($_GET['url']) . '</b> could not be found. Please re-check the URL; If you were directed here using a link, please report that link.'));
}
} else {
die(zipError('Template Not Found', 'The template <b>' . $zip['Template']['Front'] . '</b> could not be found. Please check your configuration file for a mistake.'));
}
}
我还需要提供其他信息吗?我需要尽快完成这项工作,所以任何建议将不胜感激。
最佳答案
在包含该文件之前,您可以将第二个参数 ( {username} ) 设为局部变量,然后您可以在其中将其作为普通变量进行访问。如果您与团队合作,这不是一件好事。
编辑:
您可以执行以下操作
RewriteEngine On
RewriteRule ^ index.php
然后在index.php中你可以得到这个
$url = str_replace('index.php','', $_SERVER['PHP_SELF']);
$url = str_replace($url,'',$_SERVER['REQUEST_URI']);
$url = explode('/',$url);
$page = array_shift($url);
foreach ($url as $val){
$args[] = urldecode($val);
}
现在,如果您打开链接www.website.com/profile/someuser/about
,您将获得
$page = 'profile'
$args[0] = 'someuser'
$args[1] = 'about'
依此类推,您可以有任意数量的争论。
关于php - 如何使用$_GET参数来制作seo友好的url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17015271/