2

how can i remove prefix url value i.e slug= from the url is there any process please assist if there is i tried using str_pos but does not work

my url is http://localhost/blog/?slug=what-is-lorem-ipsum

what i expect http://localhost/blog/what-is-lorem-ipsum

here is my code

<?php
include('connect.php');
if(isset($_REQUEST['slug'])){
    $sql = "SELECT * FROM `posts` WHERE `blog_slug` = '".$_REQUEST['slug']."'";
    $result = mysql_query($sql) or die( "MySQL Error: ".mysql_error() );
    $num_rows=mysql_num_rows($result);
    if($num_rows>0){
        while($rows = mysql_fetch_array($result)){
            echo "<h3><a href='http://localhost/blog/?slug=".$rows['blog_slug']."'>".$rows['blog_title']."</a></h3>";
            echo "<p>".$rows['blog_con']."</p>";
        }
    } else {
          echo "Post Not Found.";
    }
} else {
    $sql = "SELECT * FROM `posts`";
    $result = mysql_query($sql) or die( "MySQL Error: ".mysql_error() );
    $num_rows=mysql_num_rows($result);
    if($num_rows>0){
        while($rows = mysql_fetch_array($result)){
            echo "<h3><a href='http://localhost/blog/?slug=".$rows['blog_slug']."'>".$rows['blog_title']."</a></h3>";
        }
    } else {
          echo "Post Not Found.";
    } 
}
?>
Tajmin
  • 353
  • 1
  • 7
  • 23
Jimmy
  • 205
  • 1
  • 2
  • 10

4 Answers4

1

As others pointed out, you'll need some rewrite rules, e.g. with Apache's mod_rewrite module. Put this in a file called .htaccess:

RewriteEngine On
RewriteRule ^blog/(.+)$ /index.php?slug=$1 [L]

This will redirect urls like /blog/test123 to index.php?slug=test123, you'll certainly need to edit it to meet your requirements, afterwards, you can echo the new urls with PHP.

Hint: This is a rather common task and is likely to work better with some kind of Framework, i.e. with Wordpress, Slim, CodeIgniter or Laravel. Additionally, please update to mysqli_-functions or PDO as mysql_ has been deprecated centuries ago...

Jan
  • 42,290
  • 8
  • 54
  • 79
0

you can use str_replace and remove the ?slug= from the url.

$url = "<h3><a href='http://localhost/blog/?slug=".$rows['blog_slug']
$url = str_replace("?slug=", "", $url);
echo $url ." >".$rows['blog_title']."</a></h3>";

or just remove the "?slug=" from the string.

$url = "<h3><a href=http://localhost/blog/".$rows['blog_slug']; echo $url ." >".$rows['blog_title']."</a></h3>";
0

$url = "http://localhost/blog/?slug=what-is-lorem-ipsum" list($one, $two) = explode("?slug=", $url); echo $one.$two;//will echo: http://localhost/blog/what-is-lorem-ipsum

You can try this:

<a href="https//:divlancer.com">Another good solution you can find here</a>
-1

You can use the explode() function to take out what you want, and then rejoin the separated peaces:

$url = "http://localhost/blog/?slug=what-is-lorem-ipsum"
list($one, $two) = explode("?slug=", $url);
echo $one.$two;//will echo: http://localhost/blog/what-is-lorem-ipsum
Webeng
  • 7,050
  • 4
  • 31
  • 59
  • yes but it should says not found because slug is getting is response it should have to call – Jimmy May 24 '16 at 10:46