Rule for . htaccess

Asked

Viewed 78 times

0

I need the final url of my page accepted ., numbers, letters and underline however in the format I’ve been trying not to work, php returns me in the value of the parameter .php (???)

Example, the final url will be product/28.01157A_2
What is the solution in this case?

RewriteRule ^produto\/?([-\w.]+)\/?$ produto.php?referencia=$1

1 answer

1

Rewrite need not "escape" the / with \ (also does not affect anything to have) in this specific case, your middle bar does not even need the ? (indicating the previous group or character as optional).

The problem that is returning .php is precisely because of the ? after the bar /, as you "said" pro rewrite which is optional in the first rule it directs to /produto.php?referencia=123123, but here comes the value problem /produto.php also "house" (match) with the regex ^produto\/?([-\w.]+)\/?$, see a test in regex101:

So what happens are two internal redirects:

  • First that directs produto/28.01157A_2 for produto.php?referencia=28.01157A_2
  • Second that redirects produto.php?referencia=28.01157A_2 for produto.php?referencia=.php

Removing the ? already resolves:

RewriteRule ^produto/([-\w.]+)/?$ produto.php?referencia=$1

And preferably, as you’re manipulating the querystring, add the flag QSA and to avoid conflict with other later rules add the flag L

RewriteRule ^produto/([-\w.]+)/?$ produto.php?referencia=$1 [QSA,L]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.