Repetition.. repetition = bad?

Discuss practical ways rearrange URLs using mod_rewrite.

Repetition.. repetition = bad?

Postby Vincjenzo » Tue Oct 20, 2009 11:34 pm

Hi everyone. I'm having a bit of trouble in the complex world of mod_rewrite. I'm still a newbie when it comes to this.

The problem is I'm repeating my rules too often I think. Repeating code is never a good thing right, so I hope you can help me.

These are my current rewrite rules:

Code: Select all
<IfModule mod_rewrite.c>
# Turn on the furnace.
    RewriteEngine on

# Search for a brand
    RewriteRule supplier/([a-z_0-9]+) index.php?var[]=search&b=$1 [NC]

# Search for a product (numbers only!)
    RewriteRule product/([0-9]+) index.php?var[]=product&var[]=$1 [NC]

# 1-1-1/(abc-12)_(abc-12)_(abc-12)
    RewriteRule ^([0-9]+)-([0-9]+)-([0-9]+)/([a-z0-9-]+)_([a-z0-9-]+)_([a-z0-9-]+) index.php?var[]=search&tree[]=$1&tree[]=$2&tree[]=$3&c=$4&s[]=$5&s[]=$6 [NC]

# 1-1/(abc-12)_(abc-12)_(abc-12)
    RewriteRule ^([0-9]+)-([0-9]+)/([a-z0-9]+)_([a-z0-9-]+)_([a-z0-9-]+) index.php?var[]=search&tree[]=$1&tree[]=$2&c=$3&s[]=$4&s[]=$5 [NC]

# 1-1-1/(a_bc-12)
    RewriteRule ^([0-9]+)-([0-9]+)-([0-9]+)/([a-z_0-9-]+) index.php?var[]=search&tree[]=$1&tree[]=$2&tree[]=$3&&c=$4 [NC]

# 1-1/(a_bc-12)
    RewriteRule ^([0-9]+)-([0-9]+)/([a-z_0-9-]+) index.php?var[]=search&tree[]=$1&tree[]=$2&c=$3 [NC]

# 1/(a_bc-12)
    RewriteRule ^([0-9]+)/([a-z_0-9-]+) index.php?var[]=search&tree[]=$1&c=$2 [NC]


My links look like: <-- mainmenu <-- submenu ... sdram.html <-- 3rd level (and so on)

My links for showing a product look like: <-- product Id

Or, to keep the menu links 'open' when someone clicks a link in one of the overviews:

Or request all product by a supplier:

Basicly the menu can have an 'unlimited' amount of levels. This would cause me to have many of the same rewrite rules and might lead to an unmaintainable mess.

I'd appreciate any suggestions,
Posts: 1
Joined: Tue Oct 20, 2009 11:17 pm

Postby richardk » Wed Oct 21, 2009 10:07 am

Pass them together to PHP and explode() them.
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 4 guests