search

Home  >  Q&A  >  body text

RewriteRule splits the last character of the parameter

Here I try to rewrite the parameters in .htaccess and make the last parameter optional:

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

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

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

If I only enter one parameter, how can I make the first parameter full and the second parameter empty: http://site.lt/produktas/the-glass?

P粉081360775P粉081360775328 days ago470

reply all(1)I'll reply

  • P粉541796322

    P粉5417963222024-01-11 15:11:06

    Your regular expression pattern is incorrect due to the use of the quantifier and the optional / in between.

    You can use this rule:

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

    reply
    0
  • Cancelreply