×

Please Login or Register to continue.

2
(2.9K Views)

I want to create a link with id and slug using PHP. The URL should contain the post id and slug in PHP. I created a MYSQL database table and one auto-increment id. The id will autoincrement on every insertion and also I have a slug column in my database table. I want to create SEO friendly URL with post id and post slug . 
Example:

https://technosmarter.com/qa/829/how-to-connect-database-in-php-pdo

Here if only ID or Slug is entered, it will redirect to the 404 page.

CREATE TABLE `post` (
  `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `title` varchar(70) NULL,
  `slug` varchar(70) NULL,
  `content` varchar(100) NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

index.php

<?php include 'db.php'; ?>

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>TITLE</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/css/bootstrap.min.css" rel="stylesheet">
  </head>
  <body>

<?php
$result = $conn->prepare("SELECT * FROM post");
$result->execute();
$rows = $result->fetchAll(PDO::FETCH_ASSOC);
?>

<div class="container">
	<div class="row">
	
		<?php foreach ($rows as $row) { ?>
		
		<div class="col-lg-6 mt-5">
			<a href="single.php?id=<?php echo $row['id']; ?>/<?php echo $row['slug']; ?>"><h2><?php echo $row['title']; ?></h2></a>
			</p><?php echo $row['content']; ?></p>
		</div>

		<?php } ?>

	</div>
</div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/js/bootstrap.bundle.min.js"></script>
  </body>
</html>

single.php

<?php include 'db.php'; ?>

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>TITLE</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/css/bootstrap.min.css" rel="stylesheet">
  </head>
  <body>

<li class="nav-item mt-5">
	<a class="nav-link text-center" href="index.php">Home</a>
</li>

<?php
$id = $_GET['id'];
$result = $conn->prepare("SELECT * FROM post WHERE id=?");
$result->bindValue(1, $id);
$result->execute();
$posts = $result->fetch(PDO::FETCH_ASSOC);
?>

<div class="container">
	<div class="row">

		<div class="col-lg-6 mt-5">
			<a href="single.php?id=<?php echo $posts['id']; ?>/<?php echo $posts['slug']; ?>"><h2><?php echo $posts['title']; ?></h2></a>
			</p><?php echo $posts['content']; ?></p>
		</div>

	</div>
</div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/js/bootstrap.bundle.min.js"></script>
  </body>
</html>

I put it in such a way that it has a problem that can only be seen with ID.

<?php
$id = $_GET['id'];
$result = $conn->prepare("SELECT * FROM post WHERE id=?");
$result->bindValue(1, $id);
$result->execute();
$posts = $result->fetch(PDO::FETCH_ASSOC);
?>

<a href="single.php?id=<?php echo $posts['id']; ?>/<?php echo $posts['slug']; ?>"><h2><?php echo $posts['title']; ?></h2></a>

How can I use post id and post slug in link? 

I want to create a URL with an id and slug. 

(230 Points)
in PHP

Share

2 Answers
(3.4K Points)
(edited)
2

This is important and the best thing if you use post id and slug in your URL. The id should be autoincremented and the slug will be converted by the slug function. 

Here, you will find the slug() function. 

https://technosmarter.com/qa/585/i-want-to-make-seo-friendly-url-in-php

How to use post id and slug in post URL using PHP? 

You will need to use the htaccess code to do that. You are not familiar SEO friendly URLs. You can start to learn blog system in PHP

If you want to use post id and slug-like - 

example.com/id/slug 

Use htaccess code- 

Create a file and save it as .htaccess 

RewriteEngine on
RewriteRule ^([0-9]+)/([0-9a-zA-Z_-]+)$ single.php?id=$1&slug=$2 [QSA,L]

Change your URL like this - 

example.com/123/this-is-post-slug 

 Change your code line like this - 

<a href="<?php echo $row['id']; ?>/<?php echo $row['slug']; ?>"><h2><?php echo $row['title']; ?></h2></a>

or if you want a single word in your URL then create .htaccess code like this - 

RewriteEngine on
RewriteRule ^single/([0-9]+)/([0-9a-zA-Z_-]+)$ single.php?id=$1&slug=$2 [QSA,L]

Now, the URL should be like this - 

example.com/single/123/this-is-slug 

or if you use a single word in your URL then PHP code line will be like -

<a href="single/<?php echo $row['id']; ?>/<?php echo $row['slug']; ?>"><h2><?php echo $row['title']; ?></h2></a>

These are examples of SEO-friendly URLs in PHP. In this way, you can get id and slug value on your page and display related content.

Like - 

$id=$_GET['id'];
$slug=$_GET['slug'];

You can use id or slug to fetch data from the database and display it on your page.  


Comment

(9.4K Points)
1

Where is your htaccess code? You can't create slug-like example.com/post_id/post_slug without htaccess code. If you want to create a link with post id and slug using PHP then you should create an htaccess code like this - 

Create a file .htaccess 

RewriteEngine on
RewriteRule ^(.*)/$ post.php?id=$1&slug=$2 [QSA,L]

or try this 

RewriteEngine on
RewriteRule ^([0-9]+)/([0-9a-zA-Z_-]+)$ post.php?id=$1&slug=$2 [QSA,L]

Use id and slug to fetch your post. 

 You can create your Link like this - 

example.com/post/post_id/post_slug 

RewriteEngine on
RewriteRule ^post/([0-9]+)/([0-9a-zA-Z_-]+)$ post.php?id=$1&slug=$2 [QSA,L]

Check your page single.php or post.php 

You can change post.php to single.php in htaccess code. 


Comment

Related Questions :-

Featured Items:-


Certificate system in PHP website | PHP scripts

$22



Home Credito loan finance management multipurpose system in PHP

$65



Result management system with Marksheet in PHP website | PHP Scripts

$39




Your Answer

You can post your answer after login..