Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Apr 2006
    Posts
    186
    Thanks
    21
    Thanked 6 Times in 6 Posts

    Modrewriting with dashes

    I have this:
    Code:
    RewriteRule ^([a-z0-9-_]+)-card-([a-z0-9-_]+)/?$ /index.php?page=browse&cat=$1&cid=$2 [L]
    And it works fine, so the url can end up looking like:
    Code:
    mysite.com/cat1-card-15
    That works fine, but if the card id has a dash in it, it won't work:
    Code:
    mysite.com/cat1-card-15-16
    Why is that? Can it be fixed?

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    try this:

    Code:
    RewriteRule ^([a-z0-9_-]+)-card-([a-z0-9_-]+)/?$ /index.php?page=browse&cat=$1&cid=$2 [L]
    or this:

    Code:
    RewriteRule ^([a-z0-9\-_]+)-card-([a-z0-9\-_]+)/?$ /index.php?page=browse&cat=$1&cid=$2 [L]
    - is special for ranges.

    regards

  • #3
    Regular Coder
    Join Date
    Apr 2006
    Posts
    186
    Thanks
    21
    Thanked 6 Times in 6 Posts
    I tried doing those before, but they didn't work. However, I did find out my problem, it was related to my other code instead, I forgot to switch back something.

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by PeaTearGriffin View Post
    I tried doing those before, but they didn't work. However, I did find out my problem, it was related to my other code instead, I forgot to switch back something.
    it's happend,

    best regards

  • #5
    Regular Coder
    Join Date
    Apr 2006
    Posts
    186
    Thanks
    21
    Thanked 6 Times in 6 Posts
    I changed a few things and now its not working again, and it shouldn't be because of my other code, I have this now instead:

    Code:
    RewriteRule ^([a-z0-9_-]+)-([a-z0-9_-]+)-([a-z0-9_-]+)/?$ /index.php?page=$2&cat=$1&cid=$3 [L]
    So if I do this it will work as normal:
    Code:
    mysite.com/cat1-browse-15
    But once I add a dash in it, i.e. 15-16 it will not work. I did a var dump on the cid variable and noticed the result was "16" instead of "15-16" so I guess the problem is something is cutting off the hyphen and anything before it. Anyone see anything wrong with the rewrite?

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by PeaTearGriffin View Post
    I changed a few things and now its not working again, and it shouldn't be because of my other code, I have this now instead:

    Code:
    RewriteRule ^([a-z0-9_-]+)-([a-z0-9_-]+)-([a-z0-9_-]+)/?$ /index.php?page=$2&cat=$1&cid=$3 [L]
    So if I do this it will work as normal:
    Code:
    mysite.com/cat1-browse-15
    But once I add a dash in it, i.e. 15-16 it will not work. I did a var dump on the cid variable and noticed the result was "16" instead of "15-16" so I guess the problem is something is cutting off the hyphen and anything before it. Anyone see anything wrong with the rewrite?
    and page is 'browse-15' ?

    mod_rewrite is pretty limited. why don't you pass the url as a single variable and process with php?

    Code:
    RewriteRule ^(.+)$ /index.php?q=$1 [L]
    and in index.php process $_GET['q'] using preg_match or something else to extract page, cat and cid. This way, I think, you can deal more easy with cid as single item or a range.

    regards

  • #7
    Regular Coder
    Join Date
    Apr 2006
    Posts
    186
    Thanks
    21
    Thanked 6 Times in 6 Posts
    Not really sure how to do that at the moment. But I'm also looking to fix my current situation. There's nothing wrong with it? It seems like somehow the regexp is wrong or something.

  • #8
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    in my opinion it's hard to find a solution for that using regex in mod_rewrite because:
    - using 3 groups make 'cat1-browse-15-16' to be split only in 3 parts, probably 'cat1', 'browse-15' and '16'
    - using 4 groups to deal with previous case don't match, I guess, the case 'cat1-browse-15'

    try to use:
    Code:
    RewriteRule ^([a-z0-9_-]+)-([a-z0-9_]+)-([a-z0-9_-]+)/?$ /index.php?page=$2&cat=$1&cid=$3 [L]
    without dash in second group.
    but this will give, I guess, 15-16 as cid.

    regards

  • #9
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by PeaTearGriffin View Post
    Not really sure how to do that at the moment. But I'm also looking to fix my current situation. There's nothing wrong with it? It seems like somehow the regexp is wrong or something.
    I thing that a quick fix is to use what I suggest. There is a two step solution with minimal code. You can insert at top of index.php code to extract from $_GET['q'] the variables you use in the rest of the file. After that you can forget about query string.

    regards

  • #10
    Regular Coder
    Join Date
    Apr 2006
    Posts
    186
    Thanks
    21
    Thanked 6 Times in 6 Posts
    Yea, it looks like that was the problem. So now it will work, but the problem is I also have some categories that have a hypen too... So there is no workaround for that? I might try the thing you said, but how does it work for all the other urls your rewriting?

  • #11
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by PeaTearGriffin View Post
    Yea, it looks like that was the problem. So now it will work, but the problem is I also have some categories that have a hypen too... So there is no workaround for that? I might try the thing you said, but how does it work for all the other urls your rewriting?
    if other url are not redirected to index.php the only problem I see is to find the proper regex for mod_rewrite to not interact with the rest of the rules. Anyway I think is more easy to deal only with that problem then with extracting the parameters.
    This problem with hypen is another reason to use this solution.

    regards


  •  

    Posting Permissions

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