...

View Full Version : Modrewriting with dashes



PeaTearGriffin
07-23-2008, 03:39 PM
I have this:

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:

mysite.com/cat1-card-15

That works fine, but if the card id has a dash in it, it won't work:

mysite.com/cat1-card-15-16

Why is that? Can it be fixed?

oesxyl
07-23-2008, 04:03 PM
try this:


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


or this:


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


- is special for ranges.

regards

PeaTearGriffin
07-23-2008, 04:26 PM
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. :p

oesxyl
07-23-2008, 04:29 PM
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. :p
it's happend, :)

best regards

PeaTearGriffin
07-23-2008, 11:42 PM
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:



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:


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?

oesxyl
07-24-2008, 12:23 AM
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:



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:


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?



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

PeaTearGriffin
07-24-2008, 12:42 AM
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.

oesxyl
07-24-2008, 12:56 AM
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:


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

oesxyl
07-24-2008, 01:02 AM
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

PeaTearGriffin
07-24-2008, 01:18 AM
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?

oesxyl
07-24-2008, 01:29 AM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum