FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Struggling with mod_rewrite





Navigator
I simply want this:

I have index.php?category=1

and I want to convert it to

/category/my-category-name

I have checked mod_rewrite but I can't seem to find an example of how to do it!

please help!
jmraker
If you want site.com/category/my-category-name to rewrite to site.com/index.php?category=my-category-name or
you want site.com/category/1 to rewrite to site.com/index.php?category=1
Code:
RewriteEngine On
RewriteRule ^category/([A-Za-z0-9-]+)_([0-9]+)$ index.php?category=$1 [L]

If you want site.com/category/my-category-name to rewrite to site.com/index.php?category=1
you'd use
Code:
RewriteEngine On
RewriteRule ^category/my-category-name$ index.php?category=1 [L]
Navigator
jmraker wrote:
If you want site.com/category/my-category-name to rewrite to site.com/index.php?category=my-category-name or
you want site.com/category/1 to rewrite to site.com/index.php?category=1
Code:
RewriteEngine On
RewriteRule ^category/([A-Za-z0-9-]+)_([0-9]+)$ index.php?category=$1 [L]

If you want site.com/category/my-category-name to rewrite to site.com/index.php?category=1
you'd use
Code:
RewriteEngine On
RewriteRule ^category/my-category-name$ index.php?category=1 [L]


Actually a little bit different than that, the site is big enough to NOT have a rule for each page, I want to use a numeric ID and display text.

So from having this:

Code:
site.com/index.php?category=X


I want to display this:

Code:
site.com/category/category-name


I have checked this:

http://forum.modrewrite.com/viewtopic.php?p=11041#11041

And I am liking the number 4 option, but still it isn't clear. I really don't want to use text attributes as suggested by option number 3, what would you suggest?
jmraker
If you don't mind a potentially lengthy .htaccess

Code:
RewriteEngine On
RewriteRule ^category/my-category-name$ index.php?category=1 [L]
RewriteRule ^category/my-other-category-name$ index.php?category=2 [L]
RewriteRule ^category/third-cat$ index.php?category=3 [L]
RewriteRule ^category/fourth-kitty$ index.php?category=4 [L]

... and keep going for all the pages
Navigator
jmraker wrote:
If you don't mind a potentially lengthy .htaccess

Code:
RewriteEngine On
RewriteRule ^category/my-category-name$ index.php?category=1 [L]
RewriteRule ^category/my-other-category-name$ index.php?category=2 [L]
RewriteRule ^category/third-cat$ index.php?category=3 [L]
RewriteRule ^category/fourth-kitty$ index.php?category=4 [L]

... and keep going for all the pages


That's exactly what I am trying to avoid since the site is big enough NOT to do this.

Seems that I have no other elegant solution but to use the name as the parameter

Code:
RewriteRule ^category/([A-Za-z0-9-]+)_([0-9]+)$ index.php?category_name=$1
Fire Boar
You are correct, Navigator, that is your only solution. But it's not a bad one really - you can simply use a database query to map the ID to the "slug". Just make sure that the slug is unique.
Related topics
Does frihost have mod_rewrite?
mod_rewrite
.htaccess i mod_rewrite
mod_rewrite in Apache and how does it work?
.htaccess instead of mod_rewrite
[SOLVED] Using mod_rewrite.
only .htaccess instead of mod_rewrite
problema con el mod_rewrite en wmserver 3.3
75FRIH$ Mod_Rewrite Code
mod_rewrite
mod_rewrite - jak sie tego uzywa ? i czy mamy to na frihost
Can you target a <div> like a frame?
URL Cloaking / mod_rewrite advice needed
Is mod_rewrite enabled?
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.