php.htaccesscontent-management-systemslugpretty-urls

.htaccess appending URL Slugs with the anchor tags


I am creating a blog in Core PHP to get a better understand of the core functionalities of CMS. However, I am facing a challenge in the menu when I am displaying a single post.

I am using the below .htaccess code

# 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]

The above code will convert www.example.com/post.php?category=some&slug=title-ti to www.example.com/some/title-ti

Now what's happening is when i am on the index page i am dynamically generating menu. So the menu are as per the slugs.

Technology - usage-tips - overview

the above menu URLs will be like www.example.com/technology, `www.example.com/usage-tips', 'www.example.com/overview'

but when I open single post page

the URL that I have is www.example.com/technology/demo-post

and the Menu URL's that I get is

the above menu URLs will be like www.example.com/technology/technology, `www.example.com/technology/usage-tips', 'www.example.com/technology/overview' and more going into it is like an endless loop.

If I inspect the element it's www.example.com/usage-tips and so on, but when I am clicking or hovering the links then I am redirected to www.example.com/technology/usage-tips links

What's going on is there an error with my php code or the .htaccess code

following is my dynamic header code

$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;
        }

This is my post.php code

<?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
        }
?>

Solution

  • You want to be using absolute-path reference this way the browser will start from the root of your site, rather than the current directory. To use that start the href with a leading /.

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