John Davidson

php - how to rewrite url via .htaccess

0 comments
Message:


I want when someone view an ad for example https://clouditmarket.com.ng/ad/AD84772012 it should show clouditmarket.com.ng/Removal-of-Teath


Example


The question is how do i create url on my database and point to it on my code below and what is the right way to rewrite the .htaccess file to do this.


I really need your help because i understand that this will help me optimize my website ranking on google search engine.


Thank you all in advance.
//htaccess code


RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteRule ^ad/([^/.]+)/{0,1}$ ad_api.php?module=home&key=$1 [QSA,L]

THE AD INFO CODE


 $ad_id = $_GET['key'];
$shr = ''.$installation_path.'/ad/'.$ad_id.'';




try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


$stmt = $conn->prepare("SELECT * FROM tbl_ads LEFT JOIN tbl_users ON tbl_ads.author = tbl_users.user_id WHERE tbl_ads.ad_id = :adid");
$stmt->bindParam(':adid', $ad_id);
$stmt->execute();
$result = $stmt->fetchAll();
$rec = count($result);

if ($rec == "1") {
$not_found = "NO";
}else{
$not_found = "YES";
$title = "Not Found";

}

foreach($result as $row)
{
$title = $row['title'];
$category = $row['category'];
$city = $row['city'];
$adcond = $row['ad_condition'];
$price = $row['price'];
$fixed = $row['fixed'];
$brand = $row['brand'];
$des = $row['description'];
$post_date = $row['date_posted'];
$short_des = $row['short_desc'];

$author = $row['username'];
$avatar = $row['avatar'];
$verified = $row['verified'];
$email = $row['email'];
$whatsappnum = $row['whatsapp_num'];

$authid = $row['author'];
$adstatus = $row['status'];


}

}catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}


My Answer:
To rewrite the URL via .htaccess, you can modify your existing .htaccess file as follows:

apache
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteRule ^Removal-of-Teath$ ad_api.php?module=home&key=AD84772012 [QSA,L]


In this code snippet, the URL `clouditmarket.com.ng/Removal-of-Teath` will be rewritten to `clouditmarket.com.ng/ad_api.php?module=home&key=AD84772012`.

Make sure to replace `AD84772012` with the actual ad ID that you want to use in the URL.

Additionally, in your PHP code, you can retrieve the ad ID from the URL parameter and use it to fetch the corresponding ad information from the database. Here's an example:

php
$ad_id = $_GET['key'];

try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$stmt = $conn->prepare("SELECT * FROM tbl_ads LEFT JOIN tbl_users ON tbl_ads.author = tbl_users.user_id WHERE tbl_ads.ad_id = :adid");
$stmt->bindParam(':adid', $ad_id);
$stmt->execute();
$result = $stmt->fetchAll();
$rec = count($result);

if ($rec == 1) {
$not_found = "NO";
} else {
$not_found = "YES";
$title = "Not Found";
}

foreach($result as $row) {
// Retrieve ad information from the database
}
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}


With these changes, the URL rewriting should work as expected, and you can fetch the ad information based on the ad ID in your PHP code.

Rate this post

5 of 5 based on 3872 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us