mod_rewrite infinite rules?

Posted on 2010-04-07
Medium Priority
Last Modified: 2013-11-10
Hi all.

I hope you can help. I'm not sure if this is even possible...

I'm using mod_rewrite to convert this:


into this:


using this line:

RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ page.php?level1=$1&level2=$2&level3=$3

via an .htaccess file in the website root.

Do any of you good people know of a way I could use this same technique for an unlimited number of levels of sub-pages and sub-sub-pages? In reality, it's unlikely that the page hierarchy will go further than 4 levels deep, but the CMS allows for an endless tree, so I need to accommodate this in the frontend if I can.

Any guidance would be appreciated.
Question by:capnsplatter
  • 2
  • 2
LVL 16

Accepted Solution

HackneyCab earned 1500 total points
ID: 30253940
I don't believe it's possible to achieve an indeterminate number of pattern captures using mod_rewrite. The redirect target has to be written explicitly, so there's no way that I know of which would allow you to have &levelX=$X in your target.

I have to say I'd avoid an unlimited pattern like the plague. Every one of those slashes looks like a subdirectory to Google and other search engines, and the deeper a subdirectory is, the lower the value Google tends to attach to its content.

Author Comment

ID: 32414300
Apologies for the delay responding.

Thanks for your help.

I had a feeling it wouldn't be possible.

I guess I'll just have to limit the number of levels to 3 or 4.
LVL 16

Expert Comment

ID: 32418468
Yes, I would definitely recommend thinking about a more elegant way to restructure your site (and its URLs). That will keep things simpler, easier to maintain, and better looking from the point of view of visitors, and web crawlers.

Author Closing Comment

ID: 32772861

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

589 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question