Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8929829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:45:30+00:00 2026-06-15T08:45:30+00:00

I am trying to use apache-rewrite rule to convert the below URL: http://localhost/foo/bar/news.php?id=24 Into

  • 0

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

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

Into this format:

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

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)

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]

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:45:31+00:00Added an answer on June 15, 2026 at 8:45 am

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

    The will generate urls like http://localhost/news/24

    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.

    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.

    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

    First the php part

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

    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]
    

    That should do it I think.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am beginner in PHP and Apache. I am trying to use URL Rewriting
I am trying to download an image from URL. I use the Apache Commons
I am trying to rewrite multiple pages like this ... http://subdomain.domain.com/index.php?action=printpage;topic=12345.67 To this ...
Im trying to use apache HttpClient 4.1.2 but the debugger shows org.apache.http.client.httpclient is abstract;
Background Trying to recreate StackOverflow-style pretty URLs using Apache's mod rewrite and PHP. Problem
I am trying to use the latest Apache HTTP Client (v4.x) to send a
I am trying to use Apache mod_rewrite to achieve the following: In the document
I'm trying to use Apache Lucene for tokenizing, and I am baffled at the
I'm trying to use Apache XMLRPC to manage posts at a small weblog service.
I am trying to use org.apache.commons.collections.CollectionUtils in android with following code import java.util.ArrayList; import

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.