SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2011
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    URL rewrite syntax

    I've just been having a look at this, and have gotten halfway to what I would like to do.

    So I've got it working by changing my .htaccess file to include:

    Code:
    Options +FollowSymLinks
    RewriteEngine on
    RewriteRule index/LodgeID/(.*)/ index.php?LodgeID=$1
    RewriteRule index/LodgeID/(.*) index.php?LodgeID=$1

    Which, for example, is rewriting:

    http://lodges.goodsafariguide.com/lodgedetails/index.php?LodgeID=288

    To:

    http://lodges.goodsafariguide.com/lodgedetails/index/LodgeID/288/

    And I've resolved the issue of my paths to images and CSS files going AWOL.

    What I would like to be able to do is include a variant of the lodge name in the URL.

    I do have a field in the lodges table which has a short name string which could be used, e.g.:

    africanhorsebacksafaris

    Is it possible to use rewrite the URL to:

    http://lodges.goodsafariguide.com/lodgename/

    Where 'lodgename' is stored as a field in the same lodges table as LodgeID?

    Hope that makes sense. Thanks.
    Last edited by Mittineague; Nov 23, 2013 at 19:03. Reason: delinking example URLs

  2. #2
    SitePoint Enthusiast
    Join Date
    Dec 2011
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I may have been overthinking it.

    At the moment the recordset is:

    SELECT *
    FROM lodges
    WHERE LodgeID=ParamLodgeID

    Where ParamLodgeID is:

    Type: Integer
    Default Value: -1
    Run-time Value: $_GET['LodgeID']

    So I changed the recordset to:

    SELECT *
    FROM lodges
    WHERE Lodge=ParamLodgename

    Type: Text
    Default Value: -1
    Run-time Value: $_GET['Lodgename']

    It was a case of sucking it to see, but I wasn't sure about the default value being -1 for text rather than an integer.

    And I added:

    Code:
    RewriteRule index/GSG_URL/(.*)/ index.php?GSG_URL=$1
    RewriteRule index/GSG_URL/(.*) index.php?GSG_URL=$1
    RewriteRule index/(.*)/ index.php?GSG_URL=$1
    RewriteRule index/(.*) index.php?GSG_URL=$1
    to the .htaccess file.

    And that gives me:

    http://lodges.goodsafariguide.com/lodgedetails/index/GSG_URL/africanhorseback/

    and

    http://lodges.goodsafariguide.com/lodgedetails/index/africanhorseback/

    I tried:

    Code:
    RewriteRule (.*)/ index.php?GSG_URL=$1
    RewriteRule (.*) index.php?GSG_URL=$1
    To get to:

    http://lodges.goodsafariguide.com/lodgedetails/africanhorseback/

    But it didn't like that, and didn't show any data.

    Is there a tweak in the syntax, or does it always need to reference the .php page?
    Last edited by Mittineague; Nov 23, 2013 at 19:04. Reason: delinking example URLs

  3. #3
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,276
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    A short name that can take the place of an ID is typically called a "slug". And htaccess can certainly rewrite slug URLs to your PHP script, but your script will have to know what to do with a slug.

    So if you already have this...

    RewriteRule index/LodgeID/(.*)/ index.php?LodgeID=$1

    Then you could possibly do something like this...

    RewriteRule index/LodgeName/(.*)/ index.php?LodgeName=$1

    Then your PHP script will have to check for either LodgeID or LodgeName and look up the record as appropriate.

    Now with all that being said, I'd recommend a couple changes to the rewrites. I'd suggest that you add start and end anchors.

    RewriteRule ^index/LodgeID/(.*)/$ index.php?LodgeID=$1
    RewriteRule ^index/LodgeName/(.*)/$ index.php?LodgeName=$1


    The reason is because, without the anchors, then even this URL would match...

    gobbledygook/index/LodgeID/288/gobbledygook

    And next, I think we can simplify a couple rewrites...

    RewriteRule index/LodgeID/(.*)/ index.php?LodgeID=$1
    RewriteRule index/LodgeID/(.*) index.php?LodgeID=$1


    The only difference I see between these two is the trailing slash. If the goal is to make the trailing slash optional, then you can do that with the ? regex metacharacter.

    RewriteRule ^index/LodgeID/(.*)/?$ index.php?LodgeID=$1
    "First make it work. Then make it better."

  4. #4
    SitePoint Enthusiast
    Join Date
    Dec 2011
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Jeff.

    I've removed the ones I was using as I went, and now just have the one rule:

    Code:
    RewriteRule ^index/(.*)/ index.php?GSG_URL=$1
    So that gets me from:

    /lodgedetails/index.php?GSG_URL=africanhorsebacksafaris

    to:

    /lodgedetails/index/africanhorsebacksafaris/

    Is it possible to get to lose the /index/ part and get to:

    /lodgedetails/africanhorsebacksafaris/

    I tried:

    Code:
    RewriteRule ^index/(.*)/?$ index.php?GSG_URL=$1
    but it didn't like it - didn't pull through any data.

  5. #5
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,276
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by johngordon View Post
    ...

    Code:
    RewriteRule ^index/(.*)/ index.php?GSG_URL=$1
    ...

    Is it possible to get to lose the /index/ part and get to:

    /lodgedetails/africanhorsebacksafaris/
    Certainly. To lose the /index/ part from the URL, just lose the /index/ part from the rewrite.

    RewriteRule ^index/(.*)/ index.php?GSG_URL=$1
    - becomes -
    RewriteRule ^(.*)/ index.php?GSG_URL=$1

    But with one exception. It used to be that the pattern had to match "LodgeID", but now you're matching anything. Even "index.php" will match and try to rewrite to itself. To prevent that, we'll need to add a condition.

    # If the request is not for a real file...
    RewriteCond %{REQUEST_FILENAME} !-f


    # ...then rewrite
    RewriteRule ^(.*)/ index.php?GSG_URL=$1
    "First make it work. Then make it better."

  6. #6
    SitePoint Enthusiast
    Join Date
    Dec 2011
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Jeff.

    Its all working as I wanted it to now. I must have had a typo before when i tried it without the index part and its working now. (I even tried to replicate it not working by taking out the Condition line and the ^, but it still worked)

    Thanks again - this is something that has been on my mind to look at for ages so good to get it sorted.


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
  •