URL rewriting with parameter for another site . htaccess

Asked

Viewed 1,115 times

0

I’d like to write a rule on .htaccess with the RewriteRule so that when someone enters the old site, be redirected to the new site keeping the requested parameter on the old site.

Old site:

velhosite.com.br/?people=alguem
velhosite.com.br/uma-pagina-qualquer?people=alguem

Redirected to the new:

novosite.com.br/?pessoa=alguem
novosite.com.br/uma-pagina-qualquer?pessoa=alguem

Thanks in advance for the help.

2 answers

1


For redirection I use to 301:

Redirect 301 ^(www.)?velhosite\.com\.br/?people=alguem$  http://novosite.com.br/?pessoa=alguem
Redirect 301 ^(www.)?velhosite\.com\.br/uma-pagina-qualquer?people=alguem$  http://novosite.com.br/uma-pagina-qualquer?pessoa=alguem

But you can point the entire structure of the root site to a subdomain for example, and from there you make the redirects you need:

RewriteRule ^(.*)?$ http://novosite.com.br/$1 [R=301,L]

You can also make a condition, for a certain domain:

RewriteCond %{HTTP_HOST} ^(www.)?velhosite\.com\.br$
RewriteRule ^(/)?$ http://novosite.com.br/novo-layout/ [R=301,L]

For each substitution rule, you put in order:

role model: ^pagina.php?a=(.*)&b=([0-9])&c=([a-z])$, can be represented by: /a/$1/b/$2/c/$3 or /a-$1/b-$2/c-$3 or $1/$2/$3 or $1-$2-$3 or a=$1&b=$2&c=$3

You can also modify the order, example: /c/$3/b/$2/a/$1. What makes the rule recognized is the url typed in the browser, will be interpreted and trigger the original link (the rule) on the server side.

Redirect 301 ^(www.)?velhosite\.com\.br/?people=(.*)$  http://novosite.com.br/?pessoa=$1
Redirect 301 ^(www.)?velhosite\.com\.br/(.+)?people=(.*)$  http://novosite.com.br/$1?pessoa=$2

See more information about rules on apache documentation.

  • Friend, another question using the first principle that spoke where has "someone" varies by username need to pass the user to the other site and in "one-page-any" is the same thing are several pages how to send it? The old site is still active I just want to redirect who enter via "? eople=someone" understand? Help me in this... Fight...

  • I edited there @Müllernato, see if you can understand. Just pointing out that depending on the flag, or the rule, it forces everything to be typed so there are changes.

0

Good afternoon, Müller Nato.

I think what you need is the QSA Flag, Query String Append. It does nothing but add the query string, which is the part of the URL that comes after ? including the end of the rewritten url. For example: ? full=bar&bar=full

To get the query string passed to your rewritten URL, simply add this flag. For example:

old domain.conf

 RewriteEngine On
 RewriteRule ^(.*)$ http://novosite.com.br$1 [R=301,QSA,L]

R -> Flag of Redirect, 301 Permanent redirection HTTP code.

QSA -> Include the query string in the rewritten url.

L -> Last. It means that if this rule is processed, the following will be ignored.

Now that the new domain has received the rewritten url you rewrite the received url to fit its rules, like this:

new domain.

#?people=alguem
#/?pessoa=alguem
RewriteEngine On
RewriteBase /
RewriteRule ^people=(.*) /?pessoa=$1 [L]

#/uma-pagina-qualquer?people=alguem
#/uma-pagina-qualquer?pessoa=alguem
RewriteRule ^/(.*)?people=(.*) /$1?pessoa=$2 [L]

Source: https://httpd.apache.org/docs/current/rewrite/flags.html

  • You can even write a rule just to rewrite any type of url, but then you have to think about regular expression.

  • @Müller Nato Another thing, see the legal answer I found, it explains a little more the flags, then you give a read: http://answall.com/a/86900/6660

Browser other questions tagged

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