SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict crabby80's Avatar
    Join Date
    May 2007
    Posts
    387
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with RewriteRule

    Hi guys,

    I'm not entirely sure how to write RewriteRules yet

    I need to redirect anything that goes to mydomain.com/members/NAME

    to mydomain.com/members/ view.php?profile_name=NAME

    This is currently already in place at the moment, but it doesn't seem to work, is this correct?

    RewriteEngine on
    RewriteRule ^members/([^/\.]+)?/$ members/$1 [R]
    RewriteRule ^members/([^/\.]+)/?$ members/view.php?profile_name=$1

    Any help would be much appreciated

    Thanks in advance

    Crabby

  2. #2
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,604
    Mentioned
    19 Post(s)
    Tagged
    2 Thread(s)
    crabby,

    A strange attempt (pardon me) as you're wrapped around that trailing slash like another recent OP. I would have used:
    Code:
    RewriteEngine on
    # RewriteRule ^members/([^/\.]+)?/$ members/$1 [R]
    RewriteRule ^members/([^/.]+)$ members/view.php?profile_name=$1 [L]
    The differences are:

    1. By requiring a trailing / then removing it before ANDing with the "real" RewriteRule, you've twisted the logic - and likely introduced the failure point which you must be dealing with.

    2. The dot character (in a character range definition) is NOT a meta-character and does NOT need (and should NOT be) escaped.

    3. You would NOT succeed without ; or } at the end of a PHP statement or block statement so I get upset with people think that the Last flag can be omitted. It's just not logical (to me). Note: Had your first RewriteRule used a Last Flag, it would NOT have been ANDed with the second - it IS important!

    4. Just my preference: I will try to specify exactly what is allowed rather than what is not allowed, i.e., [a-z]+ rather than [^/.]+ to allow only lowercase letters. That will eliminate some error checking in the view.php script before querying the db but it makes the mod_rewrite code do some error checking for me.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  3. #3
    SitePoint Addict crabby80's Avatar
    Join Date
    May 2007
    Posts
    387
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi David,

    Thank you for your reply it's much appreciated, I'll check it out.

    Unfortunatley this was a project I took over so this code was already in place, you should see the code in the rest of the application, it's a shambles!!

    Crabby


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
  •