【问题标题】:Remove prefix url SLUG= from url从 url 中删除前缀 url SLUG=
【发布时间】:2016-05-24 10:33:13
【问题描述】:

如何从 url 中删除前缀 url 值,即 slug= 是否有任何过程,如果我尝试使用 str_pos 但不起作用,请提供帮助

我的网址是http://localhost/blog/?slug=what-is-lorem-ipsum

我的期望http://localhost/blog/what-is-lorem-ipsum

这是我的代码

<?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.";
    } 
}
?>

【问题讨论】:

  • 你要找的东西叫mod_rewrite
  • yes 告诉 webeng 进一步处理

标签: php


【解决方案1】:

正如其他人指出的那样,您需要一些重写规则,例如使用 Apache 的 mod_rewrite 模块。把它放在一个名为.htaccess的文件中:

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

这会将/blog/test123 之类的网址重定向到index.php?slug=test123,您当然需要对其进行编辑以满足您的要求,之后,您可以使用PHP 回显新网址。

提示: 这是一项相当常见的任务,并且可能在某种框架下工作得更好,即使用 WordpressSlimCodeIgniterLaravel。此外,请更新mysqli_-functions 或PDO,因为mysql_ 在几个世纪前已被弃用...

【讨论】:

    【解决方案2】:

    您可以使用 str_replace 并从 url 中删除 ?slug=。

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

    或者只是从字符串中删除“?slug=”。

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

    【讨论】:

      【解决方案3】:

      你可以使用explode()函数取出你想要的,然后重新加入分开的和平:

      $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
      

      【讨论】:

      • 是的,但它应该说未找到,因为 slug 得到的是它应该调用的响应
      猜你喜欢
      • 2018-03-20
      • 2015-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-07
      • 2018-09-10
      • 1970-01-01
      相关资源
      最近更新 更多