Reputation: 548
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:
([a-zA-Z0-9]{3,})
is not a negation of ([a-z]{2}|[a-z]{2}\-[A-Z]{2})
. URLs like http://example.com/e
or http://example.com/3n
won't match any pattern (they should match the second).http://example.com/es-US/controller/p1/p2/p3/p4/p5/p6
won't match any pattern.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
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