url-rewrite HELP :-(

supajason

Member
Messages
288
Reaction score
2
Points
18
hello i have been trying for a while and cant get this to work so maybe you can!

I have a page call "category.php"

This will just open the file
http://localhost/category/

Second
http://localhost/category.php?id={param}
http://localhost/category/{param}/

Last its
http://localhost/category.php?id={param}&page={No}
http://localhost/category/{param}/{No}

i just cant get the last bit working.

so far i have:
Code:
RewriteEngine On
RewriteRule ^category/([^/]*)$ /category.php?id=$1 [L]

Please help!
 

marshian

New Member
Messages
526
Reaction score
9
Points
0
I'm not sure this is exactly what you want, but it's worth the shot =)
By the way, I'ld drop the [L], it's nicer to have a url without query part ;-)
(also makes more search engines index that pages)

Code:
RewriteEngine On
RewriteRule ^category/(.*)/(.*)$ /category.php?id=$1&page=$2
 

woiwky

New Member
Messages
390
Reaction score
0
Points
0
The L flag just means that no more rewrite rules will be applied after that rule. Since there are no other rules here, it doesn't matter. But since you might add more later, it doesn't hurt to leave it.

Anyway, that rule won't work exactly right. It won't work with the 2 other forms. Also, I would recommend making the forward slash optional in cases where it's not needed. Else, a URL in this form for example, http://localhost/category/{id}/, won't work as expected.

If you want to do this all in one rule, try this:

Code:
RewriteEngine On
RewriteRule ^category(?(?=[^/]+)/|/?)([^/]*)(?(?=[^/]+)/|/?)([^/]*)(?(2)/?)$ /category.php?id=$1&page=$2 [L]
This should match urls in any of these forms:

http://localhost/category
http://localhost/category/
http://localhost/category/{id}
http://localhost/category/{id}/
http://localhost/category/{id}/{page}
http://localhost/category/{id}/{page}/

The only catch here is that this rule will set id and page even if they aren't given. So if your script is using isset(), it'll be true. But empty() and other tests for a false-equivalent value should still work the same. You'll need to use 3 separate rules if you don't want id and page to be set if they aren't specified.

***

I just saw your reply. I still think you should make the forward slash optional in cases where it isn't necessary. But if you're ok with it, then alright ;-)
 
Last edited:

marshian

New Member
Messages
526
Reaction score
9
Points
0
The L flag just means that no more rewrite rules will be applied after that rule. Since there are no other rules here, it doesn't matter. But since you might add more later, it doesn't hurt to leave it.

Anyway, that rule won't work exactly right. It won't work with the 2 other forms. Also, I would recommend making the forward slash optional in cases where it's not needed. Else, a URL in this form for example, http://localhost/category/{id}/, won't work as expected.

If you want to do this all in one rule, try this:

Code:
RewriteEngine On
RewriteRule ^category(?(?=[^/]+)/|/?)([^/]*)(?(?=[^/]+)/|/?)([^/]*)(?(2)/?)$ /category.php?id=$1&page=$2 [L]
This should match urls in any of these forms:

http://localhost/category
http://localhost/category/
http://localhost/category/{id}
http://localhost/category/{id}/
http://localhost/category/{id}/{page}
http://localhost/category/{id}/{page}/

The only catch here is that this rule will set id and page even if they aren't given. So if your script is using isset(), it'll be true. But empty() and other tests for a false-equivalent value should still work the same. You'll need to use 3 separate rules if you don't want id and page to be set if they aren't specified.

***

I just saw your reply. I still think you should make the forward slash optional in cases where it isn't necessary. But if you're ok with it, then alright ;-)

hmm, i was a little confused with the [R] tag xD
thought so about the other cases, but i wasn't aware of any solution, and i didn't know anything about possible multi-statement usage
 
Top