URL redirection http

Asked

Viewed 76 times

0

I’m trying to do a url redirect, but it’s not working, could someone tell me what’s wrong?

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{QUERY_STRING} ^(.*)/teste(.*)$ // verifica se tem /teste em alguma url
    RewriteRule ^(.*)$ ^/parcerias/teste.html?utm_source=teste&utm_medium=site&utm_campaign=parcerias  [R=302,L] // envia para está url
</IfModule>
  • This %{QUERY_STRING} ^(.*)/teste(.*)$ check if you have /test in Query_string, something like http://localhost/foo/bar?foo=/test, is that right what you want? Now if the goal is to reach the URL this is wrong.

  • I want http://localhost/test < to check this, after the base url of the site check if it has the test value.

  • How would I get the value after the base url and replace?

  • So QUERY_STRING doesn’t make sense, because it checks the contents of querystring as ?foo=bar and not the PATH.

  • I’m creating an answer.

  • Oh yes, I seem to understand what you mean, okay I’m waiting

  • I posted an answer, but I’m not sure what you want, test the code and if it doesn’t work explain what the goal is so I can edit the answer within your need.

  • It plays a value before the url, would know the pq of it? %5e/parcerias

  • Gustavo edited the answer, probably the sign of ^ in front of ^/parcerias/teste.html this wrong, this sign is for regex, and there is regex is the destination path only, so remove it,

  • 1

    Oops, it worked. Thank you very much!

Show 5 more comments

1 answer

2


This %{QUERY_STRING} ^(.*)/teste(.*)$ check if you have /test in QUERY_STRING, something like http://localhost/foo/bar?foo=/test, is that right what you want? Now if the goal is to reach the URL this is wrong.

Another detail, probably the sign of ^ in front of ^/parcerias/teste.html this wrong, this sign is for regex, and there is regex is the destination path only, so remove it, should stay like this:

RewriteRule ^(.*)$ /parcerias/teste.html

I believe you want it to be REQUEST_URI to verify the PATH (path in URL):

RewriteEngine On

RewriteCond %{REQUEST_URI} ^(.*)/teste(.*)$
RewriteRule ^(.*)$ /parcerias/teste.html?utm_source=teste&utm_medium=site&utm_campaign=parcerias [R=302,L]

Or simply apply to RewriteRule:

RewriteEngine On

RewriteRule ^(.*)/teste(.*)$ ^/parcerias/teste.html?utm_source=teste&utm_medium=site&utm_campaign=parcerias [R=302,L]

Of course this will probably cause a loop, since the redirected URL also has /teste in /parcerias/teste.html, you probably want otherwise to prevent redirect if it is already in a URL with /teste, then what you want is a condition of not contain /teste, in this case just use the !, thus:

RewriteEngine On

RewriteCond %{REQUEST_URI} !^(.*)/teste(.*)$
RewriteRule ^(.*)$ /parcerias/teste.html?utm_source=teste&utm_medium=site&utm_campaign=parcerias [R=302,L]

Browser other questions tagged

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