Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Regular Coder
    Join Date
    Jan 2011
    Posts
    136
    Thanks
    101
    Thanked 0 Times in 0 Posts

    Friendly url not working

    I created a small search engine (html call for php to do the query) to search a mysql database. For example, if you input 'var1' in the search box of the html, the resulting url shown on my website will be http/www.mysite.com/search.php?search=var1&submit=submit

    I am trying to figure out how to change the shown url to www.mysite.com/var1, instead.

    Anyone could help?
    Last edited by camaross; 01-22-2011 at 05:00 AM.

  • #2
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,285
    Thanks
    4
    Thanked 201 Times in 198 Posts
    Rewriting is for inbound URLs (those coming from the address bar of the browser) not for outbound URLs (those that result from coming from a form or server side language).

    What you have will take the 2nd form you want, the one with DIR/var1 in it, and change it to the original format (search.php?search=var1) not the other way around.
    Dave .... HostMonster for all of your hosting needs

  • Users who have thanked djm0219 for this post:

    toolglutton (01-21-2011)

  • #3
    120
    120 is offline
    Regular Coder
    Join Date
    Nov 2009
    Location
    UK
    Posts
    105
    Thanks
    6
    Thanked 15 Times in 15 Posts
    Dave is spot on. You are back to front. That said, if you want the URL tidy, why not just use POST data instead of GET ?
    There is always plenty of idol work for the devils hands to do
    Registered Linux User 475075 : Project Honeypot

  • #4
    New Coder
    Join Date
    Dec 2010
    Posts
    25
    Thanks
    16
    Thanked 0 Times in 0 Posts
    I have a similar question. So how do I write the rule so that the incoming /DIR/var1 will be turned into /search.php?search=var1&submit=submit?




    Quote Originally Posted by djm0219 View Post
    Rewriting is for inbound URLs (those coming from the address bar of the browser) not for outbound URLs (those that result from coming from a form or server side language).

    What you have will take the 2nd form you want, the one with DIR/var1 in it, and change it to the original format (search.php?search=var1) not the other way around.
    Last edited by toolglutton; 01-22-2011 at 04:57 AM.

  • #5
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,285
    Thanks
    4
    Thanked 201 Times in 198 Posts
    What you have should do it though I believe search=$2 needs to be search=$1 instead.
    Dave .... HostMonster for all of your hosting needs

  • #6
    New Coder
    Join Date
    Dec 2010
    Posts
    25
    Thanks
    16
    Thanked 0 Times in 0 Posts
    I do not agree with you on this.

    I do not know how to do that, but I am sure it is possible. For example www.dtcsearch.com.


    Quote Originally Posted by djm0219 View Post
    Rewriting is for inbound URLs (those coming from the address bar of the browser) not for outbound URLs (those that result from coming from a form or server side language).

    What you have will take the 2nd form you want, the one with DIR/var1 in it, and change it to the original format (search.php?search=var1) not the other way around.
    Last edited by toolglutton; 01-21-2011 at 10:47 PM.

  • #7
    Regular Coder
    Join Date
    Jan 2011
    Posts
    136
    Thanks
    101
    Thanked 0 Times in 0 Posts
    I also believe it should be able to be achieved.

    Basically my question is reduced to how to rewrite php generated urls (http://www.mysite.com/search.php?sea...ubmit=submit)?


    Quote Originally Posted by toolglutton View Post
    I do not agree with you on this.

    I do not know how to do that, but I am sure it is possible. For example www.dtcsearch.com.

  • #8
    Codeasaurus Rex
    Join Date
    Jun 2008
    Location
    Redmond, WA
    Posts
    659
    Thanks
    31
    Thanked 100 Times in 94 Posts
    Camaross, is this related to the problem solved in your other thread? Or is it new? If it's related can you update your question based on what we figured out, please. It would help me address your question.

    Toolglutton, so are you asking how to take something like http://mydomain.com/dir/var1 and send it through http://mydomain.com/search.php?search=var1 ?

    If so, you'll want to utilize something like this:

    Code:
    RewriteRule ^dir/(.*)$ search.php?search=$1 [L]
    Both of you may find this newley posted guide helpful: Mod_Rewrite How-To Guide. It's an extensive guide on how to do basic Mod_Rewrite.
    Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
    My posts, comments, code, and suggestions reflect only my personal views.
    Web Portfolio and Code Snippets: http://shanechism.com

  • Users who have thanked ShaneC for this post:

    camaross (01-22-2011)

  • #9
    Regular Coder
    Join Date
    Jan 2011
    Posts
    136
    Thanks
    101
    Thanked 0 Times in 0 Posts
    For this thread, I am trying to see if it is possible to rewrite php generated url from 'www.mysite.com/search.php?search=var1&submit=submit' to 'www.mysite.com/var1'?

    Thanks.

    [QUOTE=ShaneC;1043509]Camaross, is this related to the problem solved in your other thread? Or is it new? If it's related can you update your question based on what we figured out, please. It would help me address your question.
    Last edited by camaross; 01-22-2011 at 04:56 AM.

  • #10
    Codeasaurus Rex
    Join Date
    Jun 2008
    Location
    Redmond, WA
    Posts
    659
    Thanks
    31
    Thanked 100 Times in 94 Posts
    You shouldn't ever need to rewrite from a PHP Query String to the nice version.

    When you submit your form submit it to the clean URL. IE: http://mysite.com/var1/submit

    And then change your rewrite rules to accommodate the additional entry of /submit.

    Change your current section of the Rewrite rules from just this:
    RewriteRule ^([A-Za-z0-9-]+)/?$ search.php?search=$1 [L]

    To this:
    Code:
    RewriteRule ^([A-Za-z0-9-]+)/submit?$ search.php?search=$1&submit=submit [L]
    RewriteRule ^([A-Za-z0-9-]+)/?$ search.php?search=$1 [L]
    Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
    My posts, comments, code, and suggestions reflect only my personal views.
    Web Portfolio and Code Snippets: http://shanechism.com

  • Users who have thanked ShaneC for this post:

    camaross (01-22-2011)

  • #11
    Regular Coder
    Join Date
    Jan 2011
    Posts
    136
    Thanks
    101
    Thanked 0 Times in 0 Posts
    Thank you for the nice solution. That's refreshing.

    I am using a search box (<form action="search.php" method="get" >) inside a html to call for search.php to do the query. How do I modify them so that I can submit the form in a clean url, http://mysite.com/var1/submit?






    Quote Originally Posted by ShaneC View Post
    You shouldn't ever need to rewrite from a PHP Query String to the nice version.

    When you submit your form submit it to the clean URL. IE: http://mysite.com/var1/submit

    And then change your rewrite rules to accommodate the additional entry of /submit.

    Change your current section of the Rewrite rules from just this:
    RewriteRule ^([A-Za-z0-9-]+)/?$ search.php?search=$1 [L]

    To this:
    Code:
    RewriteRule ^([A-Za-z0-9-]+)/submit?$ search.php?search=$1&submit=submit [L]
    RewriteRule ^([A-Za-z0-9-]+)/?$ search.php?search=$1 [L]
    Last edited by camaross; 01-22-2011 at 05:13 AM.

  • #12
    Codeasaurus Rex
    Join Date
    Jun 2008
    Location
    Redmond, WA
    Posts
    659
    Thanks
    31
    Thanked 100 Times in 94 Posts
    Quote Originally Posted by camaross View Post
    Thank you for the nice solution. That's refreshing.

    I am using a search box (<form action="search.php" method="get" >) to call for search.php to do the query. How do I modify it so that I can submit the form in a clean url, http://mysite.com/var1/submit?
    Yup! When it gets to search.php you can just do something like this in your PHP:

    PHP Code:
    <?php

    if( isset( $_POST['submit'] ) ){
         
    // The form has been submitted to this address
         // Do all the neccessary POST stuff here.
    }else{
         
    // Do all the non-POST stuff here
    }

    ?>
    Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
    My posts, comments, code, and suggestions reflect only my personal views.
    Web Portfolio and Code Snippets: http://shanechism.com

  • Users who have thanked ShaneC for this post:

    camaross (01-22-2011)

  • #13
    Regular Coder
    Join Date
    Jan 2011
    Posts
    136
    Thanks
    101
    Thanked 0 Times in 0 Posts
    The url does not change. Actually, the submit input is not that important. Inside php, I can remove everything related to $_GET['submit'], and they still work fine.

    Don't you think the search.php?search=var1&submit=submit is generated by the html?


    Quote Originally Posted by ShaneC View Post
    Yup! When it gets to search.php you can just do something like this in your PHP:

    PHP Code:
    <?php

    if( isset( $_POST['submit'] ) ){
         
    // The form has been submitted to this address
         // Do all the neccessary POST stuff here.
    }else{
         
    // Do all the non-POST stuff here
    }

    ?>

  • #14
    Codeasaurus Rex
    Join Date
    Jun 2008
    Location
    Redmond, WA
    Posts
    659
    Thanks
    31
    Thanked 100 Times in 94 Posts
    Whoops! Didn't notice your method there, I thought it was POST. My apologies, I misunderstood.

    Yes, that's generated by the page and I don't believe there's a way to change that in the HTML. You can, however, cheat.

    Do like we did before, set the action to action="/var1/". You'll also need to change your Rewrite Statements to include the QSA flag.

    Take out what I just told you to put in (RewriteRule ^([A-Za-z0-9-]+)/submit?$ search.php?search=$1&submit=submit [L]). Replace that block with this:

    Code:
    RewriteRule ^([A-Za-z0-9-]+)/?$ search.php?search=$1 [L,QSA]
    Now, in your search.php, do this for me:

    PHP Code:
    <?php

    if( isset( $_GET['submit'] ) ){
         
    header'location: /' $_GET['search'] );
         exit();
    }

    // Rest of your code...
    This will then take whatever has been submitted through the form and then redirect your browser to http://mysite.com/<Search Value>.
    Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
    My posts, comments, code, and suggestions reflect only my personal views.
    Web Portfolio and Code Snippets: http://shanechism.com

  • The Following 2 Users Say Thank You to ShaneC For This Useful Post:

    camaross (01-22-2011), djh101 (01-22-2011)

  • #15
    Regular Coder
    Join Date
    Jan 2011
    Posts
    136
    Thanks
    101
    Thanked 0 Times in 0 Posts
    Thank you so much. I am working on it. One question, you mentioned to change
    Code:
    action="/var1/"
    . But there is no such thing as var1 inside the html. It is basically the <input type="text" name="search"> value.

    Am I correct?


    Quote Originally Posted by ShaneC View Post
    Whoops! Didn't notice your method there, I thought it was POST. My apologies, I misunderstood.

    Yes, that's generated by the page and I don't believe there's a way to change that in the HTML. You can, however, cheat.

    Do like we did before, set the action to action="/var1/". You'll also need to change your Rewrite Statements to include the QSA flag.

    Take out what I just told you to put in (RewriteRule ^([A-Za-z0-9-]+)/submit?$ search.php?search=$1&submit=submit [L]). Replace that block with this:

    Code:
    RewriteRule ^([A-Za-z0-9-]+)/?$ search.php?search=$1 [L,QSA]
    Now, in your search.php, do this for me:

    PHP Code:
    <?php

    if( isset( $_GET['submit'] ) ){
         
    header'location: /' $_GET['search'] );
         exit();
    }

    // Rest of your code...
    This will then take whatever has been submitted through the form and then redirect your browser to http://mysite.com/<Search Value>.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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