301 redirect out of domain with htaccess

5

I have a www.dominio1.com.br domain that points to the root of my site and another www.dominio2.com.br domain configured as a redirector for www.dominio1.com.br/novo . However, I would like the url to continue to see www.dominio2.com.br ..

I do not know anything about linux server and such, but I saw that this could be done through the .htaccess file, but after n attempts, I did not succeed.

My last attempt was:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^dominio1.pro.br/novo/$ [OR]
RewriteCond %{HTTP_HOST} ^www.dominio1.pro.br/novo/$
RewriteRule ^(.*)$ http://www.dominio2.com.br/$1 [P]

I do not know if it makes a difference, but I put this code before the next, generated by Wordpress:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /novo/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /novo/index.php [L]
</IfModule>
# END WordPress

Someone to help me?

    
asked by anonymous 02.03.2015 / 01:51

2 answers

1

First, do you want domain 2 to redirect to 1 or the other way around? What your code is trying to do is redirect from 1 pro 2, like this:

http://dominio1.com.br/novo/    ====>    http://dominio2.com.br

(In addition there is a typo, you wrote dominio1.pro.br , but I'll assume your original code is correct)

In addition, the variable HTTP_HOST does not include the path ( path ) of the request, so a query for /novo will have host only dominio1.com.br - and therefore will not activate its rules. To match both domain and path you would need to combine the HTTP_HOST rule with another rule using REQUEST_URI . I'm just quoting as an observation, because I do not think that's what you want ...

To redirect from http://dominio2.com.br to http://dominio1.com.br/novo/ you need to do the following:

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

An example of redirection with these rules would be:

http://www.dominio2.com.br/teste/    ====>    http://dominio1.com.br/novo/teste/

Tested using this online tool .

Using the flag P assumes that your server has mod_proxy . An alternative if both domains are being served by the same Apache installation is the passthrough flag PT ) as described in this documentation . These techniques, if I am not mistaken, will cause the user to continue to see www.dominio2.com.br (i.e. it is an internal redirect , not external).

However, the WordPress code will make another redirect - using only the L flag. This will cause an external redirect, which would override the first one. In the comments you said that switching from L to P causes a loop redirection, but this is not necessarily Apache's fault - second this article , WordPress uses" canonical "URLs by default, and whenever it detects that the user is not using the expected URL, he tries to redirect it using the code 301 . This may or may not explain this loop (not sure, so I suggest experimenting with the passthrough option too).

WordPress does not use relative, always absolute paths. There seems to be good reason for this, according to this answer in SOen , however it is mentioned a plugin that seems to do what you need - use relative URLs, which therefore will not change the domain as seen by the user. This plugin was designed for testing - not for use in production - but given this requirement to maintain the previous domain I believe that such a solution will be required regardless of the Apache redirect solution.

    
11.03.2015 / 01:00
0

The best way to make one domain display the other's files is by using the 301 redirect that can be done by htaccess. On CNAME of the host you also have to redirect, but you can not leave the different url appearing in the address bar in any of the cases. Using ReWrite the way you put it, it is only overwriting the new / domain1 over the new / new domain.

You can try to use it like this:

redirect 301 http//www.dominio1.com.br/novo.htm http://www.dominio2.com.br.htm

Or this way, putting the htaccess of domain1 with this excerpt:

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.dominio2.com.br/$1 [R=301,L]

Here's an article that shows you how you can use rule 301 to do targeting correctly, but it does not resolve your issue with the domain: link

In addition, you also have this very good article about specific redirect for WordPress, but it still does not solve your problem of maintaining a different url: link

I understand that you want to leave domain2.com as the domain in the address bar of the site that is hosted on domain1 / new. The problem is that from what I've seen you're using WordPress, urls should not be generated correctly and your site's header tags will not work properly. The two forms that allow it to be done as you described are with the html frameset or iframe tags, much used until the 2000s. Important to note that W3C, for example, does not recommend the use of this type of feature - or just use it if it is in the latter case. But if you were interested in the output, here is the frameset documentation: link Either way, your SEO will look badly structured.

    
11.03.2015 / 01:15