Fetch address - write it to .htaccess for NOVICE!

Discussion in 'PHP' started by le007, Sep 14, 2007.

  1. #1
    Hey all,

    say my site after going through a search comes up with this address:
    http://www.mysite.com/index.php?locate=results&city=DY&minp0&max=9&othercritera=%&moregoeshere

    how could I get it sent through .htaccess

    like with this:

    if I put this in .htaccess instead of http://www.mysite.com/index.php?locate=email

    RewriteEngine On
    RewriteRule ^email$ index.php?locate=email

    the person would only need to type:
    http://www.mysite.com/email

    Somebody please tell me how to do the same for this:
    http://www.mysite.com/index.php?locate=results&city=NYC&minp0&max=9&othercritera=%&moregoeshere

    so they'd only have to type in:
    http://www.mysite.com/nyc

    How can the PHP do this on the fly? I really dunno how to do it... fetch it from the db and write it etc - complete novice... I have a mysql db set up with the data in it already.

    Thanks!!! :cool:
     
    le007, Sep 14, 2007 IP
  2. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #2
    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]+)$ index.php?locate=results&city=$1&minp0&max=9&othercritera=%&moregoeshere

    Note the $1 which is where the city name from the request (e.g. NYC) is inserted

    Also note that this directs all request that aren't for physically existing files and directories so in your PHP, check if "city" is a valid value and if not, send a 404 as would have been the case before the rewrite
     
    krt, Sep 14, 2007 IP
  3. le007

    le007 Well-Known Member

    Messages:
    481
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #3
    Krt how you going mate -
    yeah I see your idea (being new to PHP tho) I don't know how to request_filename - oh hold on... is this standard code and the 3rd rewrite is specific to my page? What about including a state as well as a city? Would that be $2?

    Thanks for the post!!! :)
     
    le007, Sep 14, 2007 IP
  4. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #4
    The two lines starting with RewriteCond are to make sure the rule is not applied when the user is requesting a physically existing file.

    To include a state as well:

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]+)(/([^/]+))?$ index.php?locate=results&city=$1&state=$3&minp0&max=9&othercritera=%&moregoeshere

    Note I am guessing that your index.php takes a state variable

    Note this accepts requests in the form example.com/city/state

    To do it the other way around, e.g. example.com/state/city, use:

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]+)(/([^/]+))?$ index.php?locate=results&city=$3&state=$1&minp0&max=9&othercritera=%&moregoeshere

    Note the switching of $3 and $1
     
    krt, Sep 14, 2007 IP
  5. GamerJuice

    GamerJuice Peon

    Messages:
    25
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    GamerJuice, Sep 15, 2007 IP
  6. le007

    le007 Well-Known Member

    Messages:
    481
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #6
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]+)(/([^/]+))?$ index.php?locate=results&city=$1&state=$3&minp0&max=9&othercritera=%&moregoeshere

    I tried the above and when I typed in
    mywebsite.com/state

    it worked but when I typed in
    mywebsite.com/state/city it didnt

    ALSO how can I have to parsed through the php so
    mywebsite.com/state/city appears in the address rather than
    index.php?locate=results&city=$1&state=$3&minp0&max=9&othercritera=%&moregoeshere

    Thank you
     
    le007, Sep 15, 2007 IP
  7. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #7
    Recall I said that if you wanted it the other way around, i.e. mywebsite.com/state/city, use the second set of code:

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]+)(/([^/]+))?$ index.php?locate=results&city=$3&state=$1&minp0&max=9&othercritera=%&moregoeshere

    Replace ^([^-]+)(-([^-]+))?$ with ^([^/]+)(/([^/]+))?$ if you want hyphens instead of slashes to delimit the values.
     
    krt, Sep 15, 2007 IP
  8. le007

    le007 Well-Known Member

    Messages:
    481
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
  9. le007

    le007 Well-Known Member

    Messages:
    481
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #9
    Thanks for that krt... its just still, when I directly type:
    mywebsite.com/LH/1-property it doesn't work?
    mywebsite.com/LH shows all the props in that county but the /1-property part doesn't :confused:
     
    le007, Sep 16, 2007 IP
  10. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #10
    I see (you asked for LH-1-property first though ;))

    RewriteRule ^([^/]+)(/(.+)-property)?/?$ http://www.mywebsite.com/index.php?locate=viewresults&cp_county=$1&cp_town=$3&...

    Format examples:
    mywebsite.com/LH
    mywebsite.com/LH/1-property
     
    krt, Sep 16, 2007 IP
  11. le007

    le007 Well-Known Member

    Messages:
    481
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #11
    Internal Server Error
    The server encountered an internal error or misconfiguration and was unable to complete your request.

    This is whats appearing now...
    I have my .htaccess file as:

    RewriteEngine On
    RewriteRule ^register$ index.php?locate=register
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule

    ^([^/]+)(/(.+)-property)?/?$http://www.mywebsite.com/cpzr/index.php?locate=viewresults&cp_

    county=$1&cp_town=$3&minprice=0&maxprice=999999999999999&srch_property_type=%25&minbeds=1&maxbed

    s=99
     
    le007, Sep 16, 2007 IP
  12. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #12
    The error was because you joined the pattern and the URL

    It should be:
    ^([^/]+)(/(.+)-property)?/?$ http://www.mywebsite.com/...
    not:
    ^([^/]+)(/(.+)-property)?/?$http://www.mywebsite.com/...

    Also you don't need mywebsite.com in the URL... my bad for including it before.

    RewriteRule ^([^/]+)(/(.+)-property)?/?$ index.php?locate=viewresults&cp_county=$1&cp_town=$3&...
     
    krt, Sep 16, 2007 IP
  13. le007

    le007 Well-Known Member

    Messages:
    481
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #13
    Oh right, I see that now.
    Its working for county but not town-property?
     
    le007, Sep 16, 2007 IP
  14. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #14
    Can you give me an example?

    I have tested my code and mywebsite.com/LH/1-property does indeed rewrite to index.php?locate=viewresults&cp_county=lh&cp_town=1
     
    krt, Sep 16, 2007 IP
  15. loibeignacio

    loibeignacio Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Thanks a lot GamerJuice

     
    loibeignacio, Sep 17, 2007 IP
  16. le007

    le007 Well-Known Member

    Messages:
    481
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #16
    krt - I think it wasn't working because for some reason the 2nd / was making the website feel it was in a new dir and so the css file didnt work!
    RewriteRule ^([^/]+)(/(.+)-property)?/?$

    index.php?locate=viewresults&cp_county=$1&cp_town=$3&minprice=0&maxprice=999999999999999&srch_pr

    operty_type=%25&minbeds=1&maxbeds=99

    Tried this for LH-1-property but it didnt work?

    RewriteRule ^([^/]+)(-(.+)-property)?/?$

    index.php?locate=viewresults&cp_county=$1&cp_town=$3&minprice=0&maxprice=999999999999999&srch_pr

    operty_type=%25&minbeds=1&maxbeds=99
     
    le007, Sep 17, 2007 IP
  17. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #17
    I see, I forgetfully assume everyone else uses paths relative to root (as they should)

    On your site, LH/1-property is working so maybe you are using the previous rewrite rule instead of:

    RewriteRule ^([^-]+)(-(.+)-property)?/?$ index.php?locate=viewresults&cp_county=$1&cp_town=$3&minprice=0&maxprice=999999999999999&srch_property_type=%25&minbeds=1&maxbeds=99 [L]
    Code (markup):
    2 of them are used in "not" sets, e.g. [^-], which means anything except a hyphen.

    If, heaven forbid, it still doesn't work, try this:
    RewriteRule ^([^-]+)(-(.+)-property)?/?$ index.php?locate=viewresults&cp_county=$1&cp_town=$3&minprice=0&maxprice=999999999999999&srch_property_type=%25&minbeds=1&maxbeds=99 [R=302,L]
    Code (markup):
    That way, you can see what the URL is being rewritten to in the address bar. e.g. enter ...com/LH-1-property and see what it rewrites to.
     
    krt, Sep 17, 2007 IP
  18. le007

    le007 Well-Known Member

    Messages:
    481
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #18
    Hey krt,

    I changed the code to RewriteRule ^([^-]+)(-(.+)-property)?/?$ and it KIND OF worked!

    For some reason - this is VERY strange, the resulting pages are different? The same criteria is entered but the results are different?
     
    le007, Sep 18, 2007 IP
  19. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #19
    Would you be okay giving me temporary FTP access so I can get this sorted out for you instead of going back and forth for possibly awhile yet?

    Also, regardless, did you try using [R] at the end of the RewriteRule and comparing the URLs?

    Another possibility is the script is not getting vars from the URL properly.
     
    krt, Sep 19, 2007 IP