SitePoint Sponsor

User Tag List

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

    Clean URL mod rewrite problem

    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
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,095
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Awwww, you're so close! The only thing is you need to get rid of the .php in the first argument of the RewriteRule

    Code:
    RewriteRule ^read-news/(.*) read-news.php?url=$1 [L]


    PS. If you have a strict set of chracters the url field can contain, like only letters, digits and dashes, it's better to code that instead of (.*), which is quite evil (although should be harmless here, unless you have URLs that are very similar)
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2011
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry that acctually didnt work
    I was thinking maybe it was because i was using the url which stored within my database, the url field being the-news-today
    I created a test page which a normal html layout and the following php code in the body
    $url = $_GET['url'];
    echo $url;
    when i load tester.php nothing shows but if i change it to tester.php?url=hello
    the word hello appears in the screen.
    Going by your rewrite i should change read-news to tester and i should get this when i hit enter
    localhost/tutorials/rewrite/tester/hello

    I am really confused

  4. #4
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,672
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    really confused,

    This has been covered in the past ... and it DOES work (http://wilderness-wally.com):
    Code:
    RewriteRule ^([-a-zA-Z0-9_&'!\…]+)$ article.php?title=$1 [QSA,L]
    As Rémon noted, specifying all the characters allowed (within a URI so, too, within the title) is HIGHLY advisable. However, be sure to make your url field UNIQUE. Note, though, that spaces are replaced with _'s for the link and converted back for access to the database.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2011
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well whatever is wrong its not working for me. If anyone feels to be so kind my two files, htaccess and slq of my database is in the zipped link below. Iv have different people looking at it and they still cant figure it out.
    http://piercemcgeough.co.uk/rewrite-test.zip

    Please can someone be so kind to help

  6. #6
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,095
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Ah, but the first part of the URL is the same as the basename of the file supposed to serve it *light goes on in my head*

    Try adding Options -MultiViews to your .htaccess, that might solve it.

    If it doesn't, I'm out of ideas
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy


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
  •