Search code examples
php.htaccess

RewriteRule splits parameter's last character


Here I am trying to rewrite parameters in .htaccess and make last parameter optional:

RewriteRule ^produktas/([0-9a-zA-Z_-]+)/?([0-9a-zA-Z_-]+) /produktas?slug=$1&var=$2 [NC,L]

So if I would type http://site.lt/produktas/the-glass, then I expect actually to get http://site.lt/produktas?slug=the-glass, but second parameter var I expect to be empty. But when I run it I get my first parameter spit and my $_GET variable looks like this:

Array ( [slug] => the-glas [var] => s )

How can I get first parameter full and second empty if I type only one parameter like this: http://site.lt/produktas/the-glass ?


Solution

  • Your regex pattern is not correct due to quantifier + used and an optional / between.

    You may use this rule:

    RewriteRule ^produktas/([\w-]+)(?:/([\w-]+))?/?$ produktas?slug=$1&var=$2 [NC,L,QSA]