1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Removing query string from php url

Discussion in 'PHP' started by frobak, Mar 31, 2010.

  1. #1
    Hi

    I am building my own blog software and I am trying to remove a query string from a php url, for example:

    www.mysite.php/blog.php?blog_id=1&this-is-the-blog-title

    would be good if i can change to this:

    www.mysite.php/blog.php/blog_id=1/this-is-the-blog-title

    is this possible? and will it help my seo? Ive heard that bots cant navigate through links with query strings in them!

    any help or pointing in the right direction would be very much appreciated!
     
    frobak, Mar 31, 2010 IP
  2. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #2
    I do something very similar on some of my websites.

    I have a url like

    http://www.mydomain/index.php/pagename1/subpagename1/subpagename2

    
        $pages = explode('/',substr($_SERVER['REQUEST_URI'],1));
    
    PHP:
    if you do a print_r on pages you will get this

    
    Array ( [0] => index.php [1] => pagename1 [2] => subpagename1 [3] => subpagename2 ) 
    
    PHP:
    This means you can access all the parts via the array $pages.

    And if you want you can create a single rewrite rule and get rid of the index.php as well
     
    stephan2307, Mar 31, 2010 IP
    frobak likes this.
  3. frobak

    frobak Greenhorn

    Messages:
    81
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #3
    thanks for your reply. seems a little more complicated than i thought. i'll do some reading

    cheers
     
    frobak, Mar 31, 2010 IP
  4. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #4
    What is complicated? that is one line of code that you use and then if you normally have a query like this

    
    $sql = 'SELECT * FROM blog WHERE id='.$_GET['blog_id'].' LIMIT 1';
    
    PHP:
    you do it simply like this
    
    $sql = 'SELECT * FROM blog WHERE id='.$pages[1].' LIMIT 1';
    
    PHP:
    Do you really call this complicated?
     
    stephan2307, Mar 31, 2010 IP
  5. frobak

    frobak Greenhorn

    Messages:
    81
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #5
    no the url is coming from a link in a script. I have 1 page that is serving all of my blog entries, so i want it to show the the blog title coming from a variable in the url, taken from a database:

    <a class='blog_title' href='view_blog.php?post_id=$post_id&$post_title_link'>$post_title</a>

    i'm not sure how the sql would affect the page url? which is:

    $sql = "SELECT * FROM blogs WHERE post_id = $_GET[post_id]";

    which is why i didnt really understand your comment.
     
    frobak, Mar 31, 2010 IP
  6. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #6
    Well if you do it the method I suggest then this is what will happen

    change

    <a class='blog_title' href='view_blog.php?post_id=$post_id&$post_title_link'>$post_title</a>
    $sql = "SELECT * FROM blogs WHERE post_id = $_GET[post_id]";

    to

    <a class='blog_title' href='view_blog.php/$post_id/$post_title_link'>$post_title</a>
    $sql = "SELECT * FROM blogs WHERE post_id = $pages[1]";

    done and dusted.
     
    stephan2307, Mar 31, 2010 IP
  7. frobak

    frobak Greenhorn

    Messages:
    81
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #7
    ive changed as you said and now i get this:

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1?
     
    frobak, Mar 31, 2010 IP
  8. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #8
    Ok have you done some debugging?

    please post the following:

    The actual url displayed in the address bar
    The explode function
    The print_r of the array
    The SQL you tried to run
     
    stephan2307, Mar 31, 2010 IP
  9. frobak

    frobak Greenhorn

    Messages:
    81
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #9
    the sql: $sql = "SELECT * FROM blogs WHERE post_id = $pages[1]";

    whats an explode function? how do i run that?

    how do i print_r the array?
     
    Last edited: Mar 31, 2010
    frobak, Mar 31, 2010 IP
  10. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #10
    OK lets take it really easy as you seem not to get things.

    1. I provided you with some code
    
    $pages = explode('/',substr($_SERVER['REQUEST_URI'],1));
    
    PHP:
    Have you done any changes to this? If so please paste your code.

    2. Lets check if that code above works. put the code below right after the above code
    
    print_r($pages);
    
    PHP:
    3. Lets check that the sql is cone properly. After you build the sql place this code
    
    echo $sql;
    
    PHP:
     
    stephan2307, Mar 31, 2010 IP
  11. frobak

    frobak Greenhorn

    Messages:
    81
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #11
    right, sorry but i missed out a vital piece of code:

    $pages = explode('/',substr($_SERVER['REQUEST_URI'],1));

    i added this before my sql statement and now it works.

    cheers for your help and patience!!
     
    frobak, Mar 31, 2010 IP
  12. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #12
    No problem. Since appear to do web development let me give you a small advice that can save you hours and hours of banging your head against the wall if something doesn't work.

    DEBUG!!!

    Always output code stuff that will show you if values are properly carried over and functions return correct info.

    Glad it works.
     
    stephan2307, Mar 31, 2010 IP
  13. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #13
    stephan2307, Mar 31, 2010 IP
  14. frobak

    frobak Greenhorn

    Messages:
    81
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #14
    debug..i get ya, lol!! im quite new so just soaking everything up!

    ill have a go at changing the url as suggested!

    cheers again!
     
    frobak, Mar 31, 2010 IP