I am a new member in stackoverflow and beginner for php. I started one sample blog project. It works fine, but I want to change url for seo friendly. Can any one help me please? How to write htaccess code for this blog.
index.php
<?php
include_once("db.php");
$sql = mysql_query("SELECT * FROM post");
?>
<html>
<head>
<title>
Post
</title>
</head>
<body>
<h1>Post List</h1>
<ul>
<?php
while($row = mysql_fetch_array($sql)){
?>
<li><a href="post.php?pid=<?php echo $row['id']; ?>"><?php echo $row['title']; ?></a></li>
<?php }?>
</ul>
</body>
</html>
post.php
<?php
include("db.php");
if(isset($_GET['pid'])){
$id = $_GET['pid'];
$qry = mysql_query("SELECT * FROM post WHERE id=".$id);
}
if($qry === FALSE) {
die(mysql_error()); // TODO: better error handling
}
?>
<html>
<head>
<title>
View Post
</title>
<style>
body{
background-color: #c9c9c9;
}
h1, .para{
border: 2px solid red;
padding:10px;
}
</style>
</head>
<body>
<?php
while($row1 = mysql_fetch_array($qry)){
?>
<h1><?php echo $row1['title']; ?> </h1>
<p class="para"><?php echo $row1['desc']; ?></p>
<?php }?>
</body>
</html>
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^post/([0-9]+) post.php?pid=$1 [NC]
</IfModule>
When i’m running this code, url shows like this:
http://localhost/blog/post.php?pid=1
I want to display url like this
http://localhost/blog/post/1
Solution is executed successfully, but css styles not working. Css styles placed in css folder
3
Answers
try this in htaccess file
and you can use a bootsrap class
Try using –
.htaccess is used to parse the url, its not to generate the URL. You need to rewrite line
to
and your .htaccess will work