• <bdo id='iR6we'></bdo><ul id='iR6we'></ul>
      <legend id='iR6we'><style id='iR6we'><dir id='iR6we'><q id='iR6we'></q></dir></style></legend>
      <i id='iR6we'><tr id='iR6we'><dt id='iR6we'><q id='iR6we'><span id='iR6we'><b id='iR6we'><form id='iR6we'><ins id='iR6we'></ins><ul id='iR6we'></ul><sub id='iR6we'></sub></form><legend id='iR6we'></legend><bdo id='iR6we'><pre id='iR6we'><center id='iR6we'></center></pre></bdo></b><th id='iR6we'></th></span></q></dt></tr></i><div id='iR6we'><tfoot id='iR6we'></tfoot><dl id='iR6we'><fieldset id='iR6we'></fieldset></dl></div>
      <tfoot id='iR6we'></tfoot>

    1. <small id='iR6we'></small><noframes id='iR6we'>

      1. Apache URL 重写无法正常工作

        时间:2023-09-24
      2. <legend id='aGPbd'><style id='aGPbd'><dir id='aGPbd'><q id='aGPbd'></q></dir></style></legend>

        <i id='aGPbd'><tr id='aGPbd'><dt id='aGPbd'><q id='aGPbd'><span id='aGPbd'><b id='aGPbd'><form id='aGPbd'><ins id='aGPbd'></ins><ul id='aGPbd'></ul><sub id='aGPbd'></sub></form><legend id='aGPbd'></legend><bdo id='aGPbd'><pre id='aGPbd'><center id='aGPbd'></center></pre></bdo></b><th id='aGPbd'></th></span></q></dt></tr></i><div id='aGPbd'><tfoot id='aGPbd'></tfoot><dl id='aGPbd'><fieldset id='aGPbd'></fieldset></dl></div>

            <tbody id='aGPbd'></tbody>
          1. <tfoot id='aGPbd'></tfoot>

            1. <small id='aGPbd'></small><noframes id='aGPbd'>

                  <bdo id='aGPbd'></bdo><ul id='aGPbd'></ul>
                  本文介绍了Apache URL 重写无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试使用 apache-rewrite 规则来转换以下 URL:

                  I am trying to use apache-rewrite rule to convert the below URL:

                  http://localhost/foo/bar/news.php?id=24

                  变成这样的格式:

                  http://localhost/foo/bar/news/foo-bar

                  数字 24 是 MySQL 表中随机行的 id,其中还包含 titlecontent 字段.

                  The number 24 is an id of a random row from a MySQL table, which also contains title and content fields.

                  MariaDB [blog]> select * from articles;
                  +----+---------+----------+ 
                  | id | title   | content  |
                  +----+---------+----------+ 
                  |  1 | foo-bar | bla bla  | 
                  +----+---------+----------+ 
                  1 row in set (0.00 sec)
                  

                  我的 .htaccess 中有以下规则:

                  I have the following rule inside my .htaccess:

                  RewriteEngine On
                  RewriteCond %{REQUEST_FILENAME} !-d
                  RewriteCond %{REQUEST_FILENAME} !-f
                  RewriteCond %{REQUEST_FILENAME} !-l
                  ^news/([A_Za_z0_9_]+)$ DIRECTORY/AID/news.php?id=$1 [QSA,L]
                  

                  我也有一个 php 代码可以生成这样的链接:

                  I also have a php code that generates a link like this:

                  $link = "<a href='news.php?id={$row['id']}'></a>";
                  echo $link;  
                  

                  但是,我无法获得将路径更改为所需最终结果的重写规则.

                  However, I can't get the rewrite rule to change the path as the desired end result.

                  推荐答案

                  首先,您需要更改 html 中的 href,以提供新的 url 格式

                  First of all, you would need to change the href in the html, to give the new url format

                  function news_preview() {
                  $query = "SELECT * FROM news ORDER BY id DESC LIMIT 5  ";
                  $result = mysql_query($query) or die(mysql_error());
                  while($row=mysql_fetch_array($result)) 
                  {   
                    echo "<a href="/news/$row[id]">  ". substr($row['title'], 0,26)."...</a><br/>".; }
                  }
                  

                  将生成像 http://localhost/news/24

                  请注意,我从 url 中删除了 /DIRECTORY/AID,因为 htaccess 建议您希望它是 url,而不是您在文本中陈述的内容.

                  Note that I removed the /DIRECTORY/AID from the url, as the htaccess suggest you want that to be url, as opposed to what you stated in the text.

                  但是现在可以访问 http://localhost/news/this_is_article_title 类型的 url.因为 this_is_article_title 和 id 24 之间没有相关性,实现这一点的唯一方法是将 id 也添加到 url,或者让 php 查找数据库中具有此标题的新闻文章.

                  But now the get to the http://localhost/news/this_is_article_title type of url. Because there is no correlation between this_is_article_title and the id 24, the only way to achieve this is by either adding the id to the url too, or to have the php lookup the news-article with this title in the database.

                  然而,这最后一个解决方案有一些问题,因为你不能只给我们一个 URL 中的标题.你必须转义字符.此外,您还必须为数据库中的标题行添加索引以获得更好的性能.

                  This last solution however has some problems, as the you can't just us the title in a url. You have to escape characters. Also you'll have to add a index for the title row in the DB for better performance.

                  所以我将采用第一个解决方案.我们将生成这样的网址

                  So I'll go with the first solution. We will generate urls like this

                  http://localhost/news/24/this_is_article_title

                  首先是php部分

                  function news_preview() {
                  $query = "SELECT * FROM news ORDER BY id DESC LIMIT 5  ";
                  $result = mysql_query($query) or die(mysql_error());
                  while($row=mysql_fetch_array($result)) 
                  { 
                    $url = "/news/$row[id]/".preg_replace('/[^a-zA-Z0-9-_]/', '_', $row['title']);  
                    echo "<a href="$url">  ". substr($row['title'], 0,26)."...</a><br/>".; }
                  }
                  

                  接下来是 htaccess 部分.

                  Next comes the htaccess part.

                  RewriteEngine On
                  RewriteCond %{REQUEST_FILENAME} !-d
                  RewriteCond %{REQUEST_FILENAME} !-f
                  RewriteCond %{REQUEST_FILENAME} !-l
                  RewriteRule ^news/([0-9]+)/([A-Za-z0-9_-]+)$ DIRECTORY/AID/news.php?id=$1 [QSA,L]
                  

                  我认为应该可以.

                  这篇关于Apache URL 重写无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  <small id='QMvo6'></small><noframes id='QMvo6'>

                • <tfoot id='QMvo6'></tfoot>

                      <tbody id='QMvo6'></tbody>
                    • <bdo id='QMvo6'></bdo><ul id='QMvo6'></ul>
                      <i id='QMvo6'><tr id='QMvo6'><dt id='QMvo6'><q id='QMvo6'><span id='QMvo6'><b id='QMvo6'><form id='QMvo6'><ins id='QMvo6'></ins><ul id='QMvo6'></ul><sub id='QMvo6'></sub></form><legend id='QMvo6'></legend><bdo id='QMvo6'><pre id='QMvo6'><center id='QMvo6'></center></pre></bdo></b><th id='QMvo6'></th></span></q></dt></tr></i><div id='QMvo6'><tfoot id='QMvo6'></tfoot><dl id='QMvo6'><fieldset id='QMvo6'></fieldset></dl></div>

                          <legend id='QMvo6'><style id='QMvo6'><dir id='QMvo6'><q id='QMvo6'></q></dir></style></legend>