Php 获取slug并用破折号写入内容

Php 获取slug并用破折号写入内容,php,.htaccess,Php,.htaccess,我在从用破折号分隔的页面获取内容时遇到问题。例如,如果我去www.example.com/home它工作正常,但是如果我去www.example.com/about us它会得到一个错误试图获取非对象的属性,正如错误所说的是关于这行的 <h1><?php echo $page->Title; ?></h1> <?php echo $page->Content; ?> 我还有一个函数,可以创建类似这样的slug function get

我在从用破折号分隔的页面获取内容时遇到问题。例如,如果我去
www.example.com/home
它工作正常,但是如果我去
www.example.com/about us
它会得到一个错误
试图获取非对象的属性,正如错误所说的是关于这行的

<h1><?php echo $page->Title; ?></h1>
<?php echo $page->Content; ?>
我还有一个函数,可以创建类似这样的slug

function getSlug($name = null) {
global $db;
if ($name) {
    $query = $db->prepare('SELECT * FROM pages WHERE `Title` = ? LIMIT 1');
    $query->execute(array($name));
    return $query->fetchObject();
}
}
function createSlug($string){
    $string = preg_replace( '/[«»""!?,.!@£$%^&*{};:()]+/', '', $string );
    $string = strtolower($string);
    $slug=preg_replace('/[^A-Za-z0-9-]+/', '-', $string);
    return $slug;
}
在我的htaccess中,我得到了规则
RewriteRule^([a-zA-Z0-9-/]+)$index.php?name=$1[L]

为了获取内容,我使用了以下代码

<?php
    include 'header.php';
    $slug = create_slug($_GET['name']);
    $page = getSlug($slug);
?>
<div class="container">
    <h1><?php echo $page->Title; ?></h1>
    <?php echo $page->Content; ?>
<?php include 'footer.php'; ?>

我真的不知道问题出在哪里。任何帮助都将不胜感激


提前谢谢。检查ModRewrite模块是否已启用,并且在.htaccess检查中,下面的一行添加到顶部

RewriteEngine On

我已经解决了这个问题,在我的数据库中添加了一行名为Slug,我认为如果我希望Slug与标题不同,这也会更好


感谢@sk8terboi87的努力

感谢@sk8terboi87的快速回复,我的htaccess看起来像这样
RewriteEngine On#RewriteBase/#将所有其他URL重写为index.php/URL RewriteRule^([a-zA-Z0-9-/]+)$index.php?name=$1[L]ErrorDocument 404 index.php
是的,那太好了。不用在htaccess中更改,而是将slug保存在db中,并简单地显示“sluggified”链接。很好!