SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    SitePoint Evangelist ColinHughes's Avatar
    Join Date
    Sep 2004
    Location
    Spain
    Posts
    473
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP or mod_rewrite

    I have a result page with a url: www.somesite.com/property/182.html I want to make this read www.somesite.com/property/london

    The 182 part is a variable passed in the url from the previous page as an id. This is then used to query the db to display the correct results, usual stuff. The query result includes the location that I would like to display in the url but this location is not unique so I cannot just swap id for location in the passed variable.

    Is it something I can to do with mod_rewrite? I have searched for an example of what I need to do but without success.



    Regards

    Colin
    Last edited by ColinHughes; Jan 14, 2009 at 12:38. Reason: Wrong Forum

  2. #2
    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)
    mod_rewrite can detect london, and redirect it to a script, lets call it index.php appended to the url like so;

    index.php?place=london

    Index.php then needs to decipher $_GET['place'] and action the corresponding id number (182).

    This post is pretty much like this one, so I shan't repeat myself.


    But, you may end up like me, wondering if london shouldn't actually be the PK in your database table ... cause life would be much simpler

    As mentioned in this discussion, if you don't mind wading through all this c*ap.

  3. #3
    SitePoint Evangelist ColinHughes's Avatar
    Join Date
    Sep 2004
    Location
    Spain
    Posts
    473
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Cups

    I have rewritten my page to output the url as:somesite.com/property/182.html?location=London

    I now need some help with mod rewrite to change this to my required output www.somesite.com/property/london and still allow the old url's which have been SERP indexed to be redirected.

    Looked at the pages you linked to and most of it went over my head.

    C

  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)
    Quote Originally Posted by Cups View Post
    This post is pretty much like this one, so I shan't repeat myself.
    That thread was finally resolved yesterday, so take another look at it and let us know if you cant get mod_rewrite working for your own case.

  5. #5
    SitePoint Zealot
    Join Date
    Oct 2007
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is most definitely a textbook case for use of mod_rewrite...

  6. #6
    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)
    I have rewritten my page to output the url as:somesite.com/property/182.html?location=London
    Actually, having re-read your OP, you want to create a page called, say, index.php which is set up to handle something like ...

    index.php?place=london

    That looks up 'london' in your database, gets 182, and then displays the data.

    Im getting deja vu ...

  7. #7
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You need another column in your database, `friendly_url` or something, which needs to be unique. Then your script can look up on that field instead of the old id (eg 182).

  8. #8
    SitePoint Evangelist ColinHughes's Avatar
    Join Date
    Sep 2004
    Location
    Spain
    Posts
    473
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My db does already contain another unique field called property ref as well as the id field.

    My index.php page GETS the id passed in the url and displays the relevant info. There are currently over 2000 entries in the db many of which are in the same location (London for example). 182 is just a unique id for a particular property.

    Just trying to get my head around the post Cups has been dealing with.

    Confused I am

    C
    Last edited by ColinHughes; Jan 15, 2009 at 09:52. Reason: Confusion

  9. #9
    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)
    Colin, I might be getting confused here, you're probably best showing us the schema for the tables in question.

    e.g. when "london" arrives as a query string, is the landing script then not supposed to then find a load of properties, one of which is #182?

    Surely
    somesite.com/property/london
    brings back all london properties ?

    and I'd guess that if property 182 was 18a Baker Street, then don't you want:
    somesite.com/18a-baker-street to act as the alias for :
    www.somesite.com/property/182.html

  10. #10
    SitePoint Evangelist ColinHughes's Avatar
    Join Date
    Sep 2004
    Location
    Spain
    Posts
    473
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK

    First off can I say that I "inherited" this site warts and all

    I have a table named PROPERTY with:

    sql table structure -- id | propertyref | location | description

    bedrooms | bathrooms and another 20 more fields.

    Page action is as follows:
    viewproperty.php generates a list of results from the db and creates hyperlinks, such as property/182.html?location=London. The property directory contains an index.php file with the landing script that then outputs all the info on the page using GET and creates the url: somesite.com/property/182.html?location=London

    So the id (182 in this case) is a unique identifier for the property.

    There is also a mod_rewrite file in the property directory that converts .php to .html

    I hope I have explained this well enough for you to understand.

    C

  11. #11
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I'd even go with something like:

    www.example.com/properties/london/
    www.example.com/properties/london/18a-baker-st/

    Since the house 'belongs' to the location in a way

    Do you have a separate locations table, or is the value 'london' in every row for the houses in London?

  12. #12
    SitePoint Evangelist ColinHughes's Avatar
    Join Date
    Sep 2004
    Location
    Spain
    Posts
    473
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Stormrider View Post
    I'd even go with something like:

    www.example.com/properties/london/
    www.example.com/properties/london/18a-baker-st/

    Since the house 'belongs' to the location in a way

    Do you have a separate locations table, or is the value 'london' in every row for the houses in London?
    Your first example www.example.com/properties/london/ would be a fine result.

    I added the ?=location to the URL to hopefully ease the way to this end.
    S

    I cannot see how I can rewrite this using mod_rewrite. How do I deal with the ?=location part
    Colin

  13. #13
    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)
    Quote Originally Posted by ColinHughes View Post
    I added the ?=location to the URL to hopefully ease the way to this end.
    I am guessing "London" appears in your property table under the column 'location'.

    This means if you were to query your database armed with only that one piece of information, then you will end up with n rows.

    So, somesite.com/properties/london

    relates to :

    viewproperties.php?location=london

    ( we can do a .htaccess which translates that for you )

    so viewproperties.php then, as you say, creates a list of properties in London, but instead of links like:

    property/182.html

    it creates links like:

    /properties/london/18a-baker-street

    which via mod-rewrite is the equivalent of:

    viewproperties.php?location=london&property=18a-baker-street

    Then your task is to make viewproperties.php:

    safely handle those GET values (cleanse them etc)
    turn them into an sql query something like:

    "select id, details, etc from property where location = '" . $_GET['location'] . '" and property_address = '" . str_replace("-"," ", $_GET[property'] ) . "'" ;

    Where: I am guessing you keep the address in a field called property_address.

    Quote Originally Posted by Cups
    .. it creates links like:

    /properties/london/18a-baker-street
    Is that what you are trying to do though?

  14. #14
    SitePoint Evangelist ColinHughes's Avatar
    Join Date
    Sep 2004
    Location
    Spain
    Posts
    473
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    viewproperty.php
    PHP Code:
    <?php
    if(!$total):
        echo
    "<tr class='txt_red11px'><td colspan=5 align=center height='20' bgcolor='#FFFFFF' >
        No Property found.</td></tr>"
    ;
    endif;
        
    $r=@mysql_query("select *  from " $prev "property " $MyQuery " order by price,bedrooms,bathrooms asc LIMIT " $offset "," .  $rowsPerPage);

        
    $j=0;
    while(
    $d=@mysql_fetch_array($r)):
        
    $rr=mysql_query("select * from " $prev "property_pics where id=" $d[id] . " limit 1");
        
    $thumb=@mysql_result($rr,0,"thumb");
        echo
    "<tr class=bodytext8pt bgcolor=#0b578b><td rowspan=3 bgcolor=#ffffff align=center valign=top><br><a href='property/".$d[id].".html?location=$d[location]' class='bodytext8pt' title='More Information'>\n\t";
    if(
    $thumb):
         echo @
    resize("pics/" $thumb,150,"alt='" $d[propertyref] . "'");
    endif;
         echo
    "</a></td>\n\t";
    if(
    $d[price]==0){$price="Upon Request";}else{$price="&euro; " $d[price];}
         echo
    "<td bgcolor=#ffffff height=25>" $d[propertyref] . "</td><td bgcolor=#ffffff height=25>";
    if(
    $d[location]){echo $d[location];}else{echo"Unknown";}
        echo 
    "</td>
        </tr>\n\t"
    ;
        echo
    "<tr bgcolor=#ffffff class=bodytext8pt>
        <td  colspan=4 bgcolor='#ffffff' height='100%' valign=top align='left'>" 
    $d[description] . "<br>";
    if(
    $d[bedrooms]):
         echo
    "<b>Bedroom :</b> " $d[bedrooms];
    endif;
    if(
    $d[bathrooms]):
         echo
    " <b>Bathroom :</b> " $d[bathrooms];
    endif;
    if(
    $d[cat_id]==1)
        {
         echo
    "<br><br><b>Price/Month :</b> " $price;

        }
    else
        {
        
         echo
    "<br><br><b>Price/Week :</b>"

    if(
    $d[low_medium_rate]==&& $d[medium_rate]==&& $d[high_season_rate]==0)
            {
         echo
    " Upon Request";
            }
    else
            {
    if(
    $d[low_medium_rate]>0):
          echo
    "<br><br>&nbsp;&nbsp;&nbsp;&nbsp;Low Season : &euro; " $d[low_medium_rate];
    endif;
    if(
    $d[medium_rate]>0):
          echo
    "<br><br>&nbsp;&nbsp;&nbsp;&nbsp;Medium Season : &euro; " $d[medium_rate];
    endif;
    if(
    $d[high_season_rate]>0):
          echo
    "<br><br>&nbsp;&nbsp;&nbsp;&nbsp;High-Season : &euro; " $d[high_season_rate];
    endif;
            }
        }
          echo 
    "</td></tr>";
    ?>
    index.php
    PHP Code:
      <?php
                $r
    =mysql_query("select * from " $prev "property where id=" $_REQUEST[id]);
                
    $d=@mysql_fetch_array($r);
                
    $rr=mysql_query("select count(*) as total from " $prev "property_pics where id=" 
                
    $d[id]);
                  
    $total=@mysql_result($rr,0,"total");
                  
    $image=array();
                  
    $rr=mysql_query("select * from " $prev "property_pics where id=" $d[id] . 
                
    " order by pid asc");
                  while(
    $dd=@mysql_fetch_array($rr)):
                
    $image[]= $dd[thumb];
                  endwhile;
                
    ?>

    <?php 
       
    echo $d[propertyref]
       echo 
    $d[property_type]
    ?>
    I need to make the displayed url for index.php show as property/location/propertyref for example: property/london/SVA186 pref without .html

    I hope this makes sense

    Colin

  15. #15
    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)
    you want urls like this:
    www.somesite. com/property/london/SVA186

    to be rewritten (ie to get their content from) to this:
    www.somesite. com/property/182.html?location=london

    Is this correct?

  16. #16
    SitePoint Evangelist ColinHughes's Avatar
    Join Date
    Sep 2004
    Location
    Spain
    Posts
    473
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    YES

    Colin

  17. #17
    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)
    Why not read this and have a go yourself.

  18. #18
    SitePoint Evangelist ColinHughes's Avatar
    Join Date
    Sep 2004
    Location
    Spain
    Posts
    473
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK I will read through this and have a go (the best way to learn). Watch this space when it all goes pearshaped

    Thanks for your input

    Colin


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
  •