homepage Welcome to WebmasterWorld Guest from 54.211.235.255
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
mod rewrite for unknown numbers of parameters
how to generate a mod_rewrite for unknown number of parameters
Gelignite




msg:4087098
 3:29 pm on Feb 25, 2010 (gmt 0)

Hi there,
I have a small menu system on my website, basically i have main categories and subcategories for each of those main categories. So my life was pretty easy until today.

Now I need to create multiple level menus and there shouldn't be maximum limit for the menu.

I solved the PHP part of this and basically what i do is; for each level I generate a parameter called $cat{$i} and my dynamic URL looks like this

www.com/?cat1=Europe&cat2=Germany&cat3=Berlin&cat4=Kreuzberg

How can i create a mod_rewrite for this condition so even if I add 50 nested folder I get to convert my url to www.com/Europe/Germany/Berlin/Kreuzberg/SchwulesMuseum/

thanks in advance,
S

 

neostar




msg:4087525
 2:57 am on Feb 26, 2010 (gmt 0)

ok here is basic of what i think u want to do:

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^/([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ /?cat1=$1&cat2=$2&cat3=$3
RewriteRule ^/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ /?cat1=$1&cat2=$2&cat3=$3


if i m not wrong then you are willing to have 50 paramenters like:
www.com/Europe/Germany/Berlin/Kreuzberg/SchwulesMuseum/.../50th_country_in_the_list
so are you really wanna do this?

- Rajendra Pondel

g1smd




msg:4087673
 8:24 am on Feb 26, 2010 (gmt 0)

If the code goes in .htaccess, RewriteRule cannot 'see' the leading slash.

Add [L] to the end of every rule.

Don't make the trailing slash optional. Serving the same content at two different URLs is Duplicate Content.

Make the URL without trailing slash the 'real' URL. Set up another rule, a redirect, so that if slash is requested, the redirect (not shown here) removes it.


What is the maximum depth?

Add one more rewrite for each 'level':

RewriteRule ^([^/.]+)$ /?cat1=$1 [L]
RewriteRule ^([^/]+)/([^/.]+)$ /?cat1=$1&cat2=$2 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/.]+)$ /?cat1=$1&cat2=$2&cat3=$3 [L]

Gelignite




msg:4087843
 2:37 pm on Feb 26, 2010 (gmt 0)

Hi,
Thank you for the responses. The problem is I don't know what would be the maximum depth, I mean, at least I was trying to create something with N number of arguments.

From your responses I assume there is no way to create such rewrite condition without knowing what would be the maximum depth ?

I guess I limit my class up to certain number submenu.

Thank you very much,
-S

jdMorgan




msg:4087873
 3:41 pm on Feb 26, 2010 (gmt 0)

A single mod_rewrite rule can handle nine parameters, based on the back-reference tokens $1 through $9. It is possible to 'expand' this using multiple rewriteconds and/or rewriterules, but such solutions can be fairly inefficient.

If nine variables proves insufficient, consider passing the "cats" as a single parameter, defined as "cat-list" and separate the category tokens within the "friendly URLs" using hyphens (which are treated as "spaces in words" by most search engines, and which are "safe" to use in a URL-path (they are not required to be URL-encoded).

Alternately, put your cat-list into a client-side cookie, where it will be passed back to the server with each HTTP request. Do be aware that cookies are limited in size as well, though -- 1024 characters(?).

mod_rewrite cannot be viewed as a scripting language; It's meant to be compact and fast for implementing well-defined URL-translation functions ("transcription" would be a more-accurate word), but it is not infinitely-flexible.

Jim

Gelignite




msg:4087987
 6:49 pm on Feb 26, 2010 (gmt 0)

Thank you very much Jim. So let me ask you this question then, if i create a variable called $cats and assign it to something like this $cats = "eu/germany/berlin" will it be recognized as subfolder by search engines once i put this next to my domain? something like below

www.com/eu/germany/berlin

Thanks in advance,
-S

Gelignite




msg:4087988
 6:52 pm on Feb 26, 2010 (gmt 0)

I am afraid I can't :)

jdMorgan




msg:4088070
 8:53 pm on Feb 26, 2010 (gmt 0)

As posted, you should use eu-germany-berlin to ease the workload when the friendly URLs are parsed by regex in mod_rewrite.

If you're on Apache 2.x, you might also want to look into using AcceptPathInfo. See the Apache core directives documentation for important details.

Jim

g1smd




msg:4088109
 10:45 pm on Feb 26, 2010 (gmt 0)

Realisticly, look at the maths.

5 choices of category, at 9 levels of folder, gives 2 million categories.

10 choices of category, at 5 levels of folder, gives 100 000 categories.

20 choices of category, at 9 levels of folder, gives 500 billion categories.

Even a huge site like ODP rarely goes beyond 7 levels.

Gelignite




msg:4088159
 11:59 pm on Feb 26, 2010 (gmt 0)

Thank you very much for all the responses. I finally finalized the my code, it works fine now.

unfortunately, since my site has been around for three years now, i couldn't use the eu-germany-berlin structure since i've used subfolders for all these years.

so I limit the admin panel to 10 nested categories and i arranged my htaccess accordingly.

Thanks again,
S

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved