Direct via . htaccess a query string

Asked

Viewed 54 times

-2

I have a system where there will be multiple users. Each user will have his own subdomain. Ex.:

usuario1.sistema.com.br 
usuario2.sistema.com.br

For this I created the subdirectories and in each of them I created one .htaccess redirecting to the system in the root folder. See:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^cliente1.sistema.com.br$ [OR]
RewriteCond %{HTTP_HOST} ^www.cliente1.sistema.com.br$
RewriteRule ^(.*)$ http://www.sistema.com.br/acesso/$1 [P]

The problem is that when it creates this redirect to the system, I cannot identify who the client is, ie if it is the cliente1 or cliente2, etc..

How could I identify this customer? I don’t have much experience with .htaccess, but I believe I could use RewriteCond %{QUERY_STRING} and in the system file take the PHP with $_REQUEST. Something like:

RewriteCond %{QUERY_STRING}^cliente=cliente1

And in PHP:

$cliente = $_REQUEST["cliente"];

If that’s the solution, how would I apply the RewriteCond %{QUERY_STRING} in my .htaccess?

1 answer

1


I set up that rule:

RewriteEngine on
RewriteRule ^cliente([0-9]+).sistema.com.br$ http://www.sistema.com.br/acesso/$1 [P]

I added ([0-9]+) any number from 0 to 9 can be one or more characters, then put at the end of access in the redirect. The test is below in the image.

inserir a descrição da imagem aqui

You can use this tool and improve to taste : https://htaccess.madewithlove.be/

Updating:

In that case Rei would just have to adapt the rule like this :

RewriteEngine on
RewriteRule ^([a-z0-9-]+).sistema.com.br$ http://www.sistema.com.br/acesso/$1 [P]

Or as you said in the question you wanted to ask a GET per client in php could be:

RewriteEngine on
RewriteRule ^([a-z0-9-]+).sistema.com.br$ http://www.sistema.com.br/acesso?cliente=$1 [P]

Any set of characters from a to z lower or zero to nine may contain dashes, and the + outside the brackets means one or more characters.

inserir a descrição da imagem aqui

I hope this helps you!

  • Hello Rafael. Thank you for the reply, but in case instead of the name be cliente1, cliente2, is the real name of the customer. Ex.: fernandopessoa.sistema.com.br, carloschagas.sistema.com.br,... How would I do? I’m sorry, I put cliente1, cliente2, as an example.

  • 1

    The person who downvoted my answer, could justify what the problem is? Once I pasted the report of the test that proves the functioning!

  • Perfect Rafael. It worked! Thank you very much!

  • 1

    Not at all my King!

Browser other questions tagged

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