SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    May 2006
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Bit confused about mod_rewrite (php related)

    I'm trying to change my urls to friendly urls, so that they appear to the user as :

    www.mydomain.com/monitors/crt/

    rather than: www.mydomain.com/products.php?id=3&cat=crt or something

    Layout
    • Hard Disks
    • Memory
    • Peripherals
    • Monitors
      • CRT
      • TFT
    • Software
    • Networking


    So when they click on Hard disks it will go to www.mydomain.com/harddisks/ and it will follow the structure of:

    www.mydomain.com/MAINCATEGORY/SUBCATEGORY

    My confusion is, in the switch file, that is run by .htaccess, how do you determine what is the main category and sub category.

    I.e. how do you know what files to include/what to show the user?

    How do you guys create a structure like this?

  2. #2
    Non-Member I87's Avatar
    Join Date
    Mar 2006
    Location
    UK
    Posts
    378
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    umm
    I don't really understand your question
    but in the switch file

    PHP Code:
    <?php

    switch($thing){

    case 
    'harddisks':
    require_once(
    'products.php?id=3&cat=harddisks');
    break;


    case 
    'memory':
    require_once(
    'products.php?id=3&cat=memory');
    break;

    }
    ?>

  3. #3
    SitePoint Member
    Join Date
    May 2006
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by I87 View Post
    umm
    I don't really understand your question
    but in the switch file

    PHP Code:
    <?php

    switch($thing){

    case 
    'harddisks':
    require_once(
    'products.php?id=3&cat=harddisks');
    break;


    case 
    'memory':
    require_once(
    'products.php?id=3&cat=memory');
    break;

    }
    ?>
    The categories are dynamic. They are controlled via the CMS and database, so they could and will change.

  4. #4
    SitePoint Member
    Join Date
    May 2006
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also what is $thing in your swith statement.

    If it's a main category:

    www.domain.com/monitors/index.php

    You'll need monitors.

    However, if it is:

    www.domain.com/monitors/crt/index.php you'll need the crt stuff.

    So how do you know if it is a main category (monitors) or sub category(crt) and what you need to include.

  5. #5
    SitePoint Addict webaddictz's Avatar
    Join Date
    Feb 2006
    Location
    Netherlands
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So how do you know if it is a main category (monitors) or sub category(crt) and what you need to include.
    Just like you do now. You're receiving that information via the GET parameters, just like it is now. If you - for example - use:

    RewriteRule ^(.*?)/(.*?)$ products.php?id=$1&cat=$2 [L]

    Now in products.php var_dump ( $_GET ). You'll see that the parameters are there, just like they are now.

  6. #6
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    be warned that mod_rewrite may send you prematurely bald.

    Having said that I finally figured out a way to do this, you basically are putting all the variables in the URL then restructuring with mod_rewrite.
    Code:
    ## htaccess
    RewriteRule ^browse/([^/]+)/?$ /browse.php?id=$1 [L]
    RewriteCond &#37;{THE_REQUEST} ^[A-Z]{3,9}\ /browse\.php\?id=([^&]+)\ HTTP/
    RewriteRule ^browse\.php$ http://www.thomasmultimedia.com.au/browse/%1? [R=301,L]
    Now I cannot fully explain to you how this works, all I know is that it does work!

    So your browse page is setup to work when requested as such "browse.php?id=catname".
    Your links point to /browse/catname and the rest is history.

    It is important to remember that whatever GET variables you use to retrieve data still need to be present in the rewritten URL. Was that comment overkill? All I know is that it took me a while to comprehend this.
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development


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
  •