【问题标题】:.htaccess appending URL Slugs with the anchor tags.htaccess 使用锚标记附加 URL Slugs
【发布时间】:2019-05-25 14:51:33
【问题描述】:

我正在使用 Core PHP 创建一个博客,以更好地了解 CMS 的核心功能。但是,当我显示单个帖子时,我在菜单中遇到了挑战。

我正在使用下面的.htaccess 代码

# code to make pretty URLS | we're using this code to achieve /category/slug
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/([\w-]+)/([\w-]+)$ app/post.php?&category=$2&slug=$3 [L,QSA]
RewriteRule ^(.+)/([\w-]+)$ app/post.php?category=$2 [L]

上面的代码会将www.example.com/post.php?category=some&slug=title-ti转换成www.example.com/some/title-ti

现在发生的事情是,当我在索引页面上时,我正在动态生成菜单。所以菜单是根据蛞蝓。

Technology - usage-tips - overview

上述菜单 URL 将类似于 www.example.com/technology、`www.example.com/usage-tips'、'www.example.com/overview'

但是当我打开单个帖子页面时

我的网址是www.example.com/technology/demo-post

我得到的菜单 URL 是

上面的菜单 URL 将像 www.example.com/technology/technology、`www.example.com/technology/usage-tips'、'www.example.com/technology/overview' 和更多进入它就像一个无限循环。

如果我检查元素是 www.example.com/usage-tips 等等,但是当我单击或悬停链接时,我会被重定向到 www.example.com/technology/usage-tips 链接

我的 php 代码或.htaccess 代码有错误

以下是我的动态标题代码

$query = ' SELECT id, name, slug FROM categories WHERE status = 1 AND parent_id = "'.$parent_id.'" ORDER BY priority ASC ';
    $connection = $this->establish_connection();
    $data = $connection->query($query);
    $connection->close();
    if($data->num_rows > 0)
        {
            $putclass = $menu = "";
            while($row = $data->fetch_assoc())
                {
                    if($arrow_status)
                        {  $putclass = "";  }
                    else
                        { $putclass = "drop";   }

                    $menu .= '<li class='.$putclass.'><a href="'.$row["slug"].'">'.$row['name'].'</a>';
                    $menu .= '<ul class="dropdown">'.$this->menu($row["id"], true).'</ul>';
                    $menu .= '</li>';
                }

            return $menu;
        }

这是我的 post.php 代码

<?php
    // print_r($_GET);
?>
<?php
    if(isset($_GET['category']) && isset($_GET['slug']))
        {
?>
            //some content if both are set
<?php
        }

    elseif(isset($_GET['category']) && !isset($_GET['slug']))
        {
?>
            //some content if only category is set
<?php
        }
?>

【问题讨论】:

    标签: php .htaccess content-management-system slug pretty-urls


    【解决方案1】:

    您希望使用absolute-path reference 这样浏览器将从您网站的根目录开始,而不是从当前目录开始。要使用它以 href 开头,并以 / 开头。

    $menu .= '<li class='.$putclass.'><a href="/'.$row["slug"].'">'.$row['name'].'</a>';
    

    【讨论】:

    • 效果很好。下次会记住的
    猜你喜欢
    • 1970-01-01
    • 2017-01-26
    • 1970-01-01
    • 1970-01-01
    • 2018-09-20
    • 1970-01-01
    • 2012-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多