Results 1 to 5 of 5

Thread: [RESOLVED] mod_rewrite

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2007
    Location
    Rochester, NY
    Posts
    111

    Resolved [RESOLVED] mod_rewrite

    Heyas, so I'm attempting to teach myself mod_rewrite in htaccess. I've been attempting to create a url rewrite like WP uses, but with just Parent/Child. So far I have:

    Code:
    RewriteRule ^directory/(.*)/$ parent.php?parcat=$1
    
    RewriteRule ^directory/(.*)/(.*)/$ child.php?parcat=$1&subcat=$2
    The parent works, but whenever I attempt to call the sub (child), it attempts to use parent instead.

    I've tried multiple ways including changing all names, setting different virtual dir's, setting the regex to [A-Za-z] (There's no numbers, so [0-9] wouldn't need to be added), and calling them both through the same page. All to no avail *sigh*...

    Any help would be greatly appreciated.

    Thanks in advance.
    Please use the search function prior to posting a question and see if someone's already answered it.
    -If I helped you, please rate me, as I'd do the same for you =)
    -Remember to select the Resolved option for your post when you've gotten the answers you need.

  2. #2
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: mod_rewrite

    I think your problem here is that .* means "any character, as many times as possible." So, it works in your first one because you're only getting one group; it doesn't work in the second one because it can never get to your second group: the first group is grabbing everything. Try this...

    Code:
    RewriteRule ^directory/([^/]*)/$ parent.php?parcat=$1
    RewriteRule ^directory/([^/]*)/([^/]*)/$ child.php?parcat=$1&subcat=$2
    [^/]* means "any character except for /, as many times as possible." When it gets to a /, it should stop grouping. At least I think that's right... I don't always gets these right on the first try.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2007
    Location
    Rochester, NY
    Posts
    111

    Re: mod_rewrite

    Awesome. Thanks, Sambo! I was looking at some tutorials for Regex, and didn't see that anywhere. Any recommendations for a good tut for this by chance?
    Please use the search function prior to posting a question and see if someone's already answered it.
    -If I helped you, please rate me, as I'd do the same for you =)
    -Remember to select the Resolved option for your post when you've gotten the answers you need.

  4. #4
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: [RESOLVED] mod_rewrite

    Regex is still a topic that I'm not entirely confident with, myself. It doesn't help that there are variations to it based on which context you're in. But, for htaccess-flavored regex, I've bookmarked and returned to this page often for a reference. It's got a lot of info and examples.

  5. #5
    Fanatic Member modpluz's Avatar
    Join Date
    Sep 2005
    Location
    Lag, NG
    Posts
    633

    Re: [RESOLVED] mod_rewrite

    Hi neato, for regex tutorials you should see - http://www.regular-expressions.info/

    @SambaNeko
    Thanks for that solution...

    Prior to now, if i encounter such problems, what i do is something similar to below

    Code:
    RewriteRule ^directory/(.*)/(.*)/$ child.php?parcat=$1&subcat=$2
    RewriteRule ^directory/(.*)/$ parent.php?parcat=$1
    so it'll always process this
    Code:
    RewriteRule ^directory/(.*)/(.*)/$ child.php?parcat=$1&subcat=$2
    before this
    Code:
    RewriteRule ^directory/(.*)/$ parent.php?parcat=$1
    thanks for that eye opener
    Last edited by modpluz; Jul 14th, 2009 at 07:29 AM.
    If you want the rabbit to hop, move the carrot - Paul Kellerman(Prison Break)

    onError GoTo http://vbforums.com



    My Bits:
    VB6: Change Column Name in MS ACCESS

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width