Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all, you would need to change the href in the html, to give the new url format</p> <pre><code>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 "&lt;a href=\"/news/$row[id]\"&gt; ". substr($row['title'], 0,26)."...&lt;/a&gt;&lt;br/&gt;".; } } </code></pre> <p>The will generate urls like <code>http://localhost/news/24</code></p> <p>Note that I removed the <code>/DIRECTORY/AID</code> from the url, as the htaccess suggest you want that to be url, as opposed to what you stated in the text.</p> <p>But now the get to the <code>http://localhost/news/this_is_article_title</code> type of url. Because there is no correlation between <code>this_is_article_title</code> and the id <code>24</code>, 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. </p> <p>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. </p> <p>So I'll go with the first solution. We will generate urls like this </p> <p><code>http://localhost/news/24/this_is_article_title</code></p> <p>First the php part</p> <pre><code>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 "&lt;a href=\"$url\"&gt; ". substr($row['title'], 0,26)."...&lt;/a&gt;&lt;br/&gt;".; } } </code></pre> <p>Next comes the htaccess part.</p> <pre><code>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] </code></pre> <p>That should do it I think.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload