SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2011
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP & MySQL .htaccess mod rewrite

    I need to create a clean url what i have is
    localhost/tutorials/rewrite/read-news.php?url=the-news-today
    and i want it to read
    localhost/tutorials/rewrite/read-news/the-news-today

    The page is called read-page.php and the content is generated from the database
    The news.php contains the links to each news article and my link is wrote like this
    <a href="read-news.php?url=' . $url . '">' . $title1. '</a>
    and it is shown like this
    localhost/tutorials/rewrite/read-news.php?url=the-news-today

    My code to get the article is
    $url = $_GET['url'];
    $sql = "SELECT * FROM news WHERE url = '$url'";

    From looking at example I thought something like this in the htaccess would sort the problem, but it does not
    RewriteRule ^read-news/(.*).php read-news.php?url=$1 [L]

    Can anyone help me out here please

  2. #2
    SitePoint Addict
    Join Date
    Apr 2011
    Posts
    266
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    Maybe this works, in htaccess:
    Code:
    RewriteRule ^/read-news/([a-zA-Z0-9_-]+) read-news.php?url=$1 [NC,L]
    When the links are created:
    Code:
    <a href="read-news/' . $url . '">' . $title1. '</a>
    Free: Web Programming Courses HTML, CSS, Flash
    Web Programming: AJAX Course and PHP-MySQL Course video Lessons
    Good JavaScript and jQuery course for beginners

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2011
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No sorry that didnt work, i dont know whats going on it should be close though

  4. #4
    SitePoint Enthusiast
    Join Date
    Aug 2010
    Location
    Perundurai, Tamilnadu, India
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think MarPlo is almost correct. Even though add "/" infront of ^/read-news in .htaccess refers to root path. May be try below one in .htaccess


    Code:
    RewriteEngine On
    
    RewriteRule ^read-news/([a-zA-Z0-9_-]+) read-news.php?url=$1 [L]

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2011
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry still no go, maybe its because the way i have the pages wrote
    the url taken from the database is wrote as the-news-today

    This is my code on the news page

    $sql = "SELECT * FROM news WHERE hide = 0";
    $result = mysql_query($sql);
    while($row = mysql_fetch_array($result)){
    $newsid = $row['id'];
    $title1 = $row['title'];
    $date = $row['date'];
    $url = $row['url'];
    $op = "<tr>";
    $op .= '<td><h2><a href="read-news.php?url=' . $url . '">' . $title1. '</a></h2></td>';
    $op .= "<td>$date</td>";
    $op .= "</tr>";
    echo $op;
    }

    and on the read news page
    $url = $_GET['url'];
    $sql = "SELECT * FROM news WHERE url = '$url'";
    $result = mysql_query($sql);
    while($row = mysql_fetch_array($result)){
    $title = $row['title'];
    $article = $row['article'];
    $date = $row['date'];
    $op = "<h2>$title</h2>\n";
    $op .= "<p><em>Posted on $date</em></p>\n";
    $op .= "<p>$article</p>\n";
    $op .= "<hr />\n";
    $op .= "<p><a href=\"news.php\">Back To News List</a></p>\n";
    echo $op;
    }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •