SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    167
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    htaccess: Need to redirect query string

    I'm trying to make...

    http://mysite.com/blog.php?post_id=22

    to...

    http://mysite.com/blog/title-of-the-blog-post

    with the rewrite rule below but it fails on me.

    RewriteRule ^blog/([^/]+)/?$ /blog.php?post_id=$1 [L]

  2. #2
    SitePoint Wizard
    Join Date
    Jul 2004
    Location
    Minneapolis, MN
    Posts
    1,924
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would try something like this:

    RewriteRule ^blog/([a-z-]+)/?$ /blog.php?post_id=$1 [L]

    Of course, if you're using the title in the URL, you're going to get the title as 'post_id'. You will need to match the title to a post in your PHP code.

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    167
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ethanp View Post
    I would try something like this:

    RewriteRule ^blog/([a-z-]+)/?$ /blog.php?post_id=$1 [L]

    Of course, if you're using the title in the URL, you're going to get the title as 'post_id'. You will need to match the title to a post in your PHP code.
    So you're saying i'll have to post the title to the post_id?

    Please walk me through this a bit.

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Here is one way adding the 'slug' to the db

    Code:
    blog_table
    ========
    id 22
    slug "title-of-the-blog-post"
    title "Title of the Blog Post"
    text_blog "blah ...
    PHP Code:
    // **security check of GET var omitted for brevity**
    $sql "select id from blog_table where slug = '. $_GET['post_id'] .'" 
    Another way is to maintain a mapper between titles and ids as say a PHP array in a static file.

    PHP Code:
    <?php
    $blog_posts
    [22] = "title-of-the-blog-post";

    $key array_search($_GET['post_id'], $blog_posts)
    Edit:


    added array_search advice

    array_search — Searches the array for a given value and returns the corresponding key if successful

    I believe there is a way of getting Apache to do the lookup vs a static file too, but I did not use it.


    ps here is a similar discussion from earlier this week

    What does everyone else do?

    Edit:

    Moved from PHP forum? huh?

  5. #5
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,650
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by unemployment View Post
    I'm trying to make...

    http://mysite.com/blog.php?post_id=22

    to...

    http://mysite.com/blog/title-of-the-blog-post

    with the rewrite rule below but it fails on me.

    RewriteRule ^blog/([^/]+)/?$ /blog.php?post_id=$1 [L]
    There was a thread some time back where this was directly addressed (as well as within my signature's tutorial - with code). Essentially, you cannot get from title-of-the-blog-post to a post_id value (without RewriteMap which required Apache config file access) so ditch post_id and use title=$1 (after converting the spaces to _'s then back to query the database). For an example, Welcome - Wilderness Wally's Americana! uses EXACTLY this technique but without the blog subdirectory. The only tricky part(s) are ensuring UNIQUE titles and ensuring that only acceptable characters are allowed in the title.

    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

  6. #6
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    167
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dklynn View Post
    There was a thread some time back where this was directly addressed (as well as within my signature's tutorial - with code). Essentially, you cannot get from title-of-the-blog-post to a post_id value (without RewriteMap which required Apache config file access) so ditch post_id and use title=$1 (after converting the spaces to _'s then back to query the database). For an example, Welcome - Wilderness Wally's Americana! uses EXACTLY this technique but without the blog subdirectory. The only tricky part(s) are ensuring UNIQUE titles and ensuring that only acceptable characters are allowed in the title.

    Regards,

    DK
    My next question was going to be... how can you ensure unique titles without an ID? How do blog engines like wordpress handle it?

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

    I use MySQL and it has the ability to make any field unique, including, of course, the title field. Set like that, even Wilderness-Wally can't duplicate titles.

    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


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
  •