Bryan
Bryan

Reputation: 29

.htaccess Rewriting Multiple Folders

I wasn't sure how to title this and after a lot of research I'm not finding what I need. Hopefully I can explain this well enough!

My URL structure is handled by index.php, which takes the entire URL, breaks apart the /'s and organizes them into an array to see where to forward the user to. As an example here.

Now the problem with most guides on htaccess is that they give examples based on using known query strings rather than pre-set query strings I'm taking the URL and breaking it up, so it can be infinitely long. As another example, we could add new sub-cats to the above.

And this would still forward properly (basically the script automatically takes in the first item to pull the template, then the last item to use as the article id to pull. Anything in between can also have its own template, and if not, uses a generic one that displays all items within its sub-category).

I'm attempting to use htaccess to take everything after mysite.com/ and pull it in as a single query to pass to index. Ex. (using the above):

URL Input: www.mysite.com/?cat1/subcat1/subcat2/subcat3/subcat4/page1

Query passed to index.php: cat1/subcat1/subcat2/subcat3/subcat4/page1

Now the goal here is to ultimately remove the "?" after mysite.com/ to clean up the URL so it's more like mysite.com/cat1/subcat1.

As for my research, I've only been able to find things based on known query strings (such as ?cat1=&subcat1=), but I need it to be dynamic as different things will have different depths. This is where my index side comes in , but I can't figure out how to get htaccess to forward the entire query as one string for analyzing.

Any help would be much appreciated, and hopefully this was clear enough to understand. If there are any questions, feel free to ask.

Thanks in advance!

UPDATE

I've gotten it somewhat working. Here's the code I'm using now:

RewriteRule  ([^/]+) index.php?%{REQUEST_URI}%{QUERY_STRING} [nc]

This works for the most part, however it always sets array items as follows:

0 => empty
1 => index.php

2 => (first part of query, ex. "cat1")

I've tried altering the above code in many ways but I guess my lack of knowledge is holding me back from being able to properly get it set up (more or less what's in array space 3 should be in space 0, and the current 0/1 (empty/index.php) should be removed.

Upvotes: 1

Views: 247

Answers (2)

Mihail Velikov
Mihail Velikov

Reputation: 566

How about
RewriteRule (.+) index.php?url=$1 [nc]

Than in your index.php you will have

<?php    echo $_GET['url']; ?>
    output: cat1/subcat1/subcat2/subcat3/subcat4/page1

and should manually split your categories.

Upvotes: 1

obesechicken13
obesechicken13

Reputation: 833

I don't really know how to solve your problem but the issue you are talking about is called url rewriting.

It's a common way to organize things in PHP and content management systems. A framework. The framework is called MVC or model view control.

I know that in cake-php, a specific framework/csm, it is entirely possible to turn off url rewriting. Though if you do this, I think it worsens search engine optimization.

Upvotes: 0

Related Questions