【问题标题】:create .htaccess file by using php mysql.使用 php mysql 创建 .htaccess 文件。
【发布时间】:2013-07-18 05:54:35
【问题描述】:

我有一个新闻文章网站。在这里,当我点击任何文章时,它的地址看起来像 news.php?id=12 但我希望它看起来像 http://paritynews.com/news/YYYY/MM/DD//headline/。 在这首先显示新闻类别,然后是文章发布日期,然后是文章编号,最后显示帖子的标题。 这里 id 链接http://www.monkks.com/part/parity-newsfinalpage/index.php 谢谢。

【问题讨论】:

    标签: php mysql html .htaccess


    【解决方案1】:

    在这里,我找到了一篇关于使用自定义 URL 的好帖子。 http://net.tutsplus.com/tutorials/other/using-htaccess-files-for-pretty-urls/

    您可以使用 RewriteRule 生成所需的 URL 类型。

    【讨论】:

      【解决方案2】:

      使用这个 .htaccess 文件和 $_REQUEST 变量。

      RewriteEngine on
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteRule . index.php [L]
      

      这是 index.php 文件。我添加了 cmets,您必须在其中实际运行查询并处理结果。

      //Routing...
      $request = substr($_SERVER['REQUEST_URI'],1); //Out of the http://www.domain.com/blog/date -> blog/date is returned
      $params  = explode("/", $request); //Creates an array $params = ['blog','date'];
      
      if($params[0] == ""){
      //Default Route
          $include_path = "default.html";
      }else{
      //If there is something in the URL that is useful
      //Parse the rest of the url here
      switch ($params[0]) {//The first varible Ex: 'blog' or 'news'
          case 'news':
              $article_date = join(array($params[1],$params[2],$params[3]),"-");//Formats the date for MySQL
              $id_number = $params[4];
              //Make sure to escpe the values here
              $sql = "SELECT * FROM table_name WHERE (date_column = {$article_date} AND id = {$id_number}";
              //Run the query with your perfered sql extension, mysqli or pdo
              //Handle errors
              $include_path = "news.php";
          break;
          case 'other':
              $include_path = "something_else.php";
              break;
          }
      }
      if(isset($include_path)){
          require($include_path);
      }
      ?>
      

      【讨论】:

      • 谢谢。但我真的对此一无所知。所以请你解释一下。请。
      猜你喜欢
      • 2019-08-11
      • 2014-12-29
      • 2011-07-18
      • 1970-01-01
      • 2013-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多