skip to Main Content

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


  1. try this in htaccess file

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
    

    and you can use a bootsrap class

    class bootstrap {
        function __construct() {
            $url=isset($_GET['url']) ? $_GET['url'] : null;
            $url=rtrim($url,'/');
            $url = explode('/',$url );
            //print_r($url);
            if(empty($url[0])) {
                require 'controllers/index.php';
                $controller = new index();
                return false;
            }
            echo $url .'<br>';
            $file = 'controllers/' .$url[0]. '.php';
            if(file_exists($file)) {
                require $file;
            }
            else {
                require 'controllers/error.php';
                $controller = new error();
                return false;
            }
    
            $controller = new $url[0];
            if(isset($url[2])) {
                $controller ->{$url[1]}($url[2]);
            }
            else {
                    if(isset($url[1])) {
                    $controller ->{$url[1]}();
                }
            }
        }
    }
    
    Login or Signup to reply.
  2. Try using –

    RewriteEngine On
    RewriteRule ^([a-zA-Z0-9_-]+)$ post.php?pid=$1
    RewriteRule ^([a-zA-Z0-9_-]+)/$ post.php?pid=$1
    
    Login or Signup to reply.
  3. .htaccess is used to parse the url, its not to generate the URL. You need to rewrite line

    <li><a href="post.php?pid=<?php echo $row['id']; ?>"><?php echo $row['title']; ?></a></li>
    

    to

    <li><a href="/blog/post/<?php echo $row['id']; ?>"><?php echo $row['title']; ?></a></li>
    

    and your .htaccess will work

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search