How to rewrite apache URL with variable and then use other variables in PHP?

Asked

Viewed 68 times

0

Hello, I don’t know if the title of the question makes much sense, but the question is this::

I have that code:

RewriteRule ^teste/([a-z]+)$ settings/php/teste.php?lib=$1

It creates a redirect, but when I try to add variables through the URL it doesn’t work:

www.meusite.com/test/{variable}? id={variable}

Specifically, whatever is after the question mark PHP does not recognize as a variable and so I cannot run my scrip. Is there a better way to configure apache so I can rewrite the URL by already adding a default variable as if it were a directory, and then use the variables in a normal way? Or what you suggest so I can use a URL this way?

  • Wouldn’t it be easier to pass /teste/{variável}/id/{variável} and treat that in the archive teste.php?

  • Yes, but I would like to use it in this way that I posted, if I do not find the solution to this problem, I will be required to do so...

  • And also, you will have several parameters not only id, so make it more complicated..

1 answer

1


Apparently the flag is missing QSA en route.

RewriteRule ^teste/([a-z]+)$ settings/php/teste.php?lib=$1 [QSA]

See the DOC:

When the Replacement URI contains a query string, the default behavior of Rewriterule is to discard the existing query string, and replace it with the Newly generated one. Using the [QSA] flag causes the query strings to be Combined.

Consider the following Rule:

Rewriterule "/pages/(.+)" "/page.php? page=$1" [QSA]

With the [QSA] flag, a request for /pages/123? one=two will be Mapped to /page.php? page=123&one=two. Without the [QSA] flag, that same request will be Mapped to /page.php? page=123 - that is, the existing query string will be discarded

  • 1

    Our dear Mt obg, saved my life!!

Browser other questions tagged

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