SitePoint Sponsor

User Tag List

Results 1 to 19 of 19
  1. #1
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Taunton, UK
    Posts
    787
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question about mod rewrite

    I am interested in replacing dynamic links on my site with something a little friendlier looking.

    My site is an accommodation directory site and each accommodation listing will have a link such as

    http://www.myaccommodationsite.com/v...on.php?id=5435

    Instead I would like to have something like

    http://www.myaccommodationsite.com/theRitzHotel

    I have read how this can be done manually by editing the htaccess file. I would however like to know if it can be done dynamically so that when a new advertiser signs up and is added to the database, can I somehow automatically update the htaccess file with this new URL? Or perhaps there is an alternative way to achieve the same results?

    Any advice much appreciated!

    Thanks

    Paul
    Mediakitchen Limited
    App Development | Website Design & Development | Flash Game Development
    Somerset, UK
    http://www.mediakitchen.co.uk

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Mod Rewrites are usually done with Regular Expressions, so are dynamic. This means you don't have to update it.

    It's done with 2 simple lines. The first isn't required if it's already there:
    Code:
    RewriteEngine On
    That primes the rewrite engine.

    Then:
    Code:
    RewriteRule ^/Accommodations/(.+?)/?$ /viewAccommodation.php?name=$1
    Then alter viewAccommodation.php to check by name if the ID isn't present, voila

    It will rewrite anything with that pattern, e.g: Accommodations/theRitzHotel is rewritten to viewAccommodation.php?name=theRitzHotel.

    If you want to be in the root, you'll need to add commands to ignore files that do actually exist, and can sometimes cause issues with misspelt files etc.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Taunton, UK
    Posts
    787
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Jake.

    Can you clarify what you mwan about viewAccommodation.php needing to check by name.

    If someone types in Accommodations/theRitzHotel then does my viewAccommodation.php page need to use the string "theRitzHotel" to determine which accommodation to display. If so then could be a problem if there are more than one accommodation in the UK with the same name - which is definitely possible I would have thought?
    Mediakitchen Limited
    App Development | Website Design & Development | Flash Game Development
    Somerset, UK
    http://www.mediakitchen.co.uk

  4. #4
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yeah, you seem to have understood my post correctly.

    What you'd do is have another column called 'FriendlyName'. This would contain a UNIQUE name. For example, say there are two accommodations called 'some hotel', one could have SomeHotel and another could have SomeHotelTaunton (For the taunton branch).

    You'd then retrieve the row by this field. This allows advertisers to choose their own urls.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  5. #5
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Taunton, UK
    Posts
    787
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cool - thank you for explaining.

    Btw is this a sensible idea on the whole or is there any reason why I should not do this?
    Mediakitchen Limited
    App Development | Website Design & Development | Flash Game Development
    Somerset, UK
    http://www.mediakitchen.co.uk

  6. #6
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    It's perfectly fine

    .HTAccess rewrites are commonly used for this stuff. Commonly developers use .htaccess to rewrite EVERY request (other than images etc) to a single file, which parses the request string and outputs HTML in a view file - giving you more power over how you handle requests. This is generally called MVC, though it doesn't always use actual MVC methodology
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  7. #7
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Taunton, UK
    Posts
    787
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am nearly there - just need a little more help.

    Here is what I have got working:

    Code:
    Options +FollowSymLinks
    RewriteEngine on
    RewriteBase /
    RewriteCond %{QUERY_STRING} ^$
    RewriteRule ^([^/]+)$ viewAccommodationDetails.php?id=$1
    Now this works if I type in my website url as follows:

    http://www.mywebsite.co.uk/5283

    However I would like to type it in as

    http://www.mywebsite.co.uk/listing/5283

    Can you assist me in getting the mod rewrite syntax correct for this?

    Btw I am just testing with using the id number for now. When I get this working I will convert to use a friendly name instead.

    Thanks

    Paul
    Mediakitchen Limited
    App Development | Website Design & Development | Flash Game Development
    Somerset, UK
    http://www.mediakitchen.co.uk

  8. #8
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Taunton, UK
    Posts
    787
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry I think I may have sussed it.

    This seems to work:

    Code:
    Options +FollowSymLinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^listings/([0-9]+)$ viewAccommodationDetails.php?id=$1
    Does this look OK?
    Mediakitchen Limited
    App Development | Website Design & Development | Flash Game Development
    Somerset, UK
    http://www.mediakitchen.co.uk

  9. #9
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yep, it looks fine.

    Although, it might not work if a slash follows the url, e.g. /listings/1412/ - so you could use:
    Code:
    RewriteRule ^listings/([0-9]+)/?$ viewAccommodationDetails.php?id=$1
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  10. #10
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Taunton, UK
    Posts
    787
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Jake

    Yes you are right - it didn't work with a slash at the end.

    Your code fixed this but it now displays as if it hasn't found the css file.

    I reference my CSS files as follows:

    Code:
    
    <link rel="stylesheet" type="text/css" media="all" href="../css/styles.css"/>
    One other thing I noticed is my form after submission displays the dynamic url again

    The form action is as follows:

    Code:
    <form name="enquiry" id="enquiry"  method="post" action="../viewAccommodationDetails.php?id=<?php echo $accommodationId; ?>#tab_3">

    Btw I use jquery tabs in case you are wondering what the tab bit is. This form is on one of the tabs
    Mediakitchen Limited
    App Development | Website Design & Development | Flash Game Development
    Somerset, UK
    http://www.mediakitchen.co.uk

  11. #11
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Now the URL has changed, so you need to reference the CSS file relative to the url seen in the browser.

    A better solution would be to reference it relative to the site root (by using the full URL).

    Code:
    <link rel="stylesheet" type="text/css" media="all" href="/css/styles.css"/>
    Code:
    <form name="enquiry" id="enquiry"  method="post" action="/listings/<?php echo $accommodationId; ?>/#tab_3">
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  12. #12
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Taunton, UK
    Posts
    787
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey thanks Jake.

    You are good! You should be charging for that knowledge

    Thanks

    Paul
    Mediakitchen Limited
    App Development | Website Design & Development | Flash Game Development
    Somerset, UK
    http://www.mediakitchen.co.uk

  13. #13
    SitePoint Member
    Join Date
    Aug 2009
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Im so glad this is a current topic.... im looking to rewrite on a global level...
    does anyone know how to do this for every vhost on a server???

    I put this at the bottom of my httpd.conf file but it isnt rewriting correctly..

    <IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule ^(.*)page([0-9]+)\.html$ $1?tpage=$2 [L]
    </IfModule>

    Do I have to put it in each vhost file, or in a <directory></directory> ...

    Is this possible?

  14. #14
    SitePoint Member
    Join Date
    Aug 2009
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    a friendly forum always attracts many people..so y dont u try and implement ur own ideas to earn more..

  15. #15
    Floridiot joebert's Avatar
    Join Date
    Mar 2004
    Location
    Kenneth City, FL
    Posts
    823
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  16. #16
    SitePoint Enthusiast SmAsH_HaCkEr's Avatar
    Join Date
    Aug 2009
    Location
    Egypt
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank Man

  17. #17
    om nom nom nom Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,271
    Mentioned
    50 Post(s)
    Tagged
    2 Thread(s)
    I should just note, for those reading: IF you have full access to a server, meaning you are root and not some hoster, do NOT use .htaccess.

    .htaccess files are for users who have their own directories but not root access (what you have on most 3rd party web hosters). However if you have access to the main configuration files use it!

    When the server is using .htaccess, it looks in every directory for .htaccess files. Every time. If your hoster has decided to use .htaccess, then so be it, that's what you use. But I've seen people using it on servers they wholly own and control. They were basically tying their feet together with rope before a race.

  18. #18
    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)
    Yeh, it's far far faster to use an apache config file than an .htaccess file!

  19. #19
    Non-Member Musicbox's Avatar
    Join Date
    Nov 2004
    Location
    india
    Posts
    1,331
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can also find coder on http://www.rentacoder.com asking to rewrite mod_rewrite for your website for $5 or more....


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
  •