Just a nice guy
Just a nice guy

Reputation: 548

Recursive htaccess rewrite rule

I use this htaccess rewriterule:

RewriteRule ^([a-z]{2}|[a-z]{2}\-[A-Z]{2})/?$ /index.php?lang=$1 [L]
RewriteRule ^([a-z]{2}|[a-z]{2}\-[A-Z]{2})/([^/]*)/?$ /index.php?lang=$1&controller=$2 [L]
RewriteRule ^([a-z]{2}|[a-z]{2}\-[A-Z]{2})/([^/]*)/([^/]*)/?$ /index.php?lang=$1&controller=$2&p1=$3 [L]
RewriteRule ^([a-z]{2}|[a-z]{2}\-[A-Z]{2})/([^/]*)/([^/]*)/([^/]*)/?$ /index.php?lang=$1&controller=$2&p1=$3&p2=$4 [L]
RewriteRule ^([a-z]{2}|[a-z]{2}\-[A-Z]{2})/([^/]*)/([^/]*)/([^/]*)/([^/]*)/?$ /index.php?lang=$1&controller=$2&p1=$3&p2=$4&p3=$5 [L]
RewriteRule ^([a-z]{2}|[a-z]{2}\-[A-Z]{2})/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/?$ /index.php?lang=$1&controller=$2&p1=$3&p2=$4&p3=$5&p4=$6 [L]
RewriteRule ^([a-z]{2}|[a-z]{2}\-[A-Z]{2})/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/?$ /index.php?lang=$1&controller=$2&p1=$3&p2=$4&p3=$5&p4=$6&p5=$7 [L]

As you can see, the logic of this is that the first parameter is language formated (ie: en or en-US), the second parameter is a the controller and the rest are parameters are named as 'p' concat n, where n is the order number of the parameter (p1/p2/p3).

Now, I want to add something like this:

RewriteRule ^([a-zA-Z0-9]{3,})/?$ /index.php?defaultcontroller=true&p1=$1 [L]
RewriteRule ^([a-zA-Z0-9]{3,})/([^/]*)/?$ /index.php?defaultcontroller=true&p1=$1&p2=$2 [L]
// And so on ...

This is trying that if the first parameter is not language formated, the controller is a default controller and the first parameter is p1, the second is p2 and so on. But this has three problems:

So, please, could you help me to reduce this to a (kind of) recursive code having only two lines (one for the first parameter is language formated and one for the first parameter non-laguage formated)?

Upvotes: 1

Views: 406

Answers (1)

Amit Verma
Amit Verma

Reputation: 41219

You can use the following 1 liner :

    RewriteRule ^(en|en-US)/?([^/]*)/?([^/]*)/?([^/]*)/?([^]*)/([^/]*)/?([^/]*)/?$ /index.php?lang=$1&controller=$2&p1=$3&p2=$4&p3=$5&p4=$6&p5=$7 [L]

Upvotes: 1

Related Questions