Forum Moderators: phranque

Message Too Old, No Replies

Recursive RewriteRule for /param1/param2/./paramN/ structure

How to rewrite /param1/param2/paramN/ to index.php?p1=param1&p2=param2...

         

karminski

9:22 am on Sep 5, 2007 (gmt 0)

10+ Year Member



Hello! Please, need help with RewriteCond & RewriteRule.

I want to rewrite my url like [host.com...] to [host.com...] Ive read about recursive mode with [N] key for RewriteRule, but I couldn't use it well.

Ive tried following code in my .htaccess file:


RewriteEngine On
RewriteOptions MaxRedirects=20
RewriteCond %{REQUEST_URI} ^(.*)/([^/]+)/([^/]+)/(.*)$
RewriteRule ^.*$ /class.%2.php?action=someaction&item[]=%3&lang=%1 [NC,N]

But this code gives me an infinite loop. My rewrite log file looks like this:

... (2) [per-dir y:/home/unistrict.ru/www/] rewrite ru/services/distribution/ -> /class.services.php?action=someaction&item[]=distribution&lang=/ru
... (2) [per-dir y:/home/unistrict.ru/www/] rewrite /class.services.php/services/distribution/ -> /class.services.php?action=someaction&item[]=distribution&lang=/ru
... (2) [per-dir y:/home/unistrict.ru/www/] rewrite /class.services.php/services/distribution/ -> /class.services.php?action=someaction&item[]=distribution&lang=/ru
... (2) [per-dir y:/home/unistrict.ru/www/] rewrite /class.services.php/services/distribution/ -> /class.services.php?action=someaction&item[]=distribution&lang=/ru

As you can see first time my rewrite rules are working good, but second times not. Please, help me with these rules!

jdMorgan

2:54 pm on Sep 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This does not look like a good problem for [N] to solve, but it is hard to be sure because I don't know the scope of the URL variations.

If the number of parameters is less than 10, then I'd suggest a series of rules, each designed to extract one parameter from the input URL.

If the number of parameters is greater than nine, then I'd suggest rewriting all appropriate requests directly to the script, and modifying the script itself to extract the parameters from server variable Request_URI.

Another possible solution, on Apache 2.0 and above, is to use AcceptPathInfo, and modify the script so that it gets the parameters from server variable Path_Info.

In your code, the back-reference is to %2; If you use [N], then you'll need to modify the URL, removing that part referred to by %2, so that the next step sees the proper variable in that %2 position.

Also, using ^.*/<specific_pattern/.*$ will be prone to problems, because the first ".*" will 'consume' as much of the URL as possible, leaving the final .* subpattern to 'starve' -- it will likely always be blank. This will also result in the input URL being processed as if it were right-justified -- In simple terms, this pattern will prefer to go all the way to the right end of the URL, and URL-parts from the beginning of the URL will be preferentially dropped. Sorry for the complex wording, but I can't think of a better way to put it. I strongly suggest using one the approaches above.

Jim

karminski

5:38 am on Sep 6, 2007 (gmt 0)

10+ Year Member



Thanks, Jim. Trying to solve my problem by your ways.

karminski

5:17 am on Sep 7, 2007 (gmt 0)

10+ Year Member



Ok. Now I find a good solution!
The good solution is send all /param1/.../paramN/ as one parameter and split it with PHP. For example, in .htaccess file you can write:

RewriteRule ^([^/]+)/(.*)$ class.$1.php?params=$2

Next step is split params in PHP:


$params = split("/", $_GET["params"]);