301 redirect outside the domain with htaccess

Asked

Viewed 2,457 times

5

I own a domain www.dominio1.com.br that points to the root of my site and another domain www.dominio2.com.br configured as redirector to www.dominio1.com.br/novo. However, I would like the user to continue seeing the URL www.dominio2.com.br ..

I don’t know anything about linux server and such, but I saw that this could be done through file . htaccess, but after n attempts, failed to 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 don’t know if it makes a difference, but I put this code before the next one, 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?

  • to no server now, I have no way to test. Have you tried using [L, NC] or [QSA,L] ?

  • Dude, I’d love to test it, but I have no idea what you’re talking about.. Hehe If I post the code I test it, but I don’t have server knowledge. Just don’t forget that I need to keep (or change) the Word Press code, which, from what I understand, keeps the links friendly..

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 to 2, so:

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

(also there is a typo, you wrote dominio1.pro.br, but I’ll assume your original code is correct in that sense)

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

To redirect from http://dominio2.com.br for 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 redirecting with these rules would be:

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

Tested using this online tool.

The use of flag P assumes that your server has the mod_proxy. An alternative if both domains are being served by the same Apache installation is passthrough (flag PT), as described in that documentation. These techniques, if I’m not mistaken, will cause the user to continue seeing the www.dominio2.com.br (i.e. is a redirect intern, and not external).

However, the Wordpress code will make another redirect - using only the flag L. This will cause an external redirect, which would override the first one. In the comments you said the L for P causes a loop redirect, but this is not necessarily the fault of Apache - second that article, Wordpress uses "canonical" Urls by default, and whenever it detects that the user is not using the expected URL it tries to redirect it using the code 301. That may or may not explain this loop (I’m not sure, so I suggest you experiment with the option passthrough also).

Wordpress does not use relative paths, always absolute. There seems to be good reasons for this, second that answer in Soen, however it is mentioned a plugin which seems to do what you need - use relative Urls, which therefore will not change the domain as viewed by the user. This plugin was designed for testing - and not for use in production - but given this requirement to maintain the previous domain I believe a solution like this will be necessary, regardless of the solution of redirecting Apache.

  • Thank you for the detailed explanation. About uses first statement, yes, I want to direct to www.dominio1.com.br/novo upon accessing www.dominio2.com.br keeping www.dominio2.com.br in the URL. Just take me another question to set correctly: this setting for redirect I do where? In the server panel I set domain 2 to redirect to www.dominio1.com.br/novo. Is that correct?

  • @Cassiomilanelo Which panel? I tried to help in what I could with regards to Apache, but I know virtually nothing about Wordpress... So I can’t tell if what you did is right or not.

  • The admin panel of my server, Locaweb, in this case. I entered my site admin panel and added www.dominio2.com.br how to redirect to www.domain1.com.br/novo, because it was the only way when someone accessed www.dominio2.com.br it go somewhere, right? (It could be by CNAME in the DNS Zone too, I believe, but I don’t think it would make a difference).

  • I put the code you told me before the WP code and the URL continued to change to www.dominio1.com.br/novo after redirecting. I changed the flags L for P from WP and went into infinite loop. Finally I tried to remove the code from WP and the same happens from the first case, the URL changes. I’d really like to know if I’m doing the redirect the right way...

  • @Cassiomilanelo In fact. Is your hosting shared, or do you have full control of Apache? In general, you need to use NameVirtualHost for the same Apache installation to serve two or more domains, but I imagine Locaweb does this for you automatically when you use their dashboard, I don’t know.

  • Hosting is shared.. A shared server and two registered domains. There’s no way to do what I need?

  • @Cassiomilanelo No problem, if the hosting is shared for sure they are using NameVirtualHost (I asked because, if it were not, you would have to do it yourself). As for your comment on the flags, I honestly don’t know, but you could follow the suggestion of that oil and use [R=301,L] and probably He’ll keep the domain on a first access. My biggest concern are the links generated by Wordpress, unless they are relative (in which case it would be all right) some additional configuration in Wordpress itself will most likely be necessary.

Show 3 more comments

0

The best way to make one domain display files from another is by using 301 redirect that can be done by htaccess. Through the host’s CNAME you can also redirect, but there is no way to let the different url appear in the address bar in either case. Using the Rewrite as you put it, you are only overwriting the new/1 domain on top of the new/1 domain.

You can try using it like this:

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

Or in this way, putting the htaccess of the domain 1 with this excerpt:

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

Here’s an article that shows how you can use the 301 rule to do the targeting correctly, but doesn’t resolve your issue with Domain: https://gesatech.net/knowledgebase/23/Create-a-301-redirect-for-all-http-requests-that-are-going-to-the-old-domain.html

What’s more, it also has this very good article about redirecting specific to Wordpress, but it still doesn’t solve your problem of maintaining a different url: http://www.agenciamestre.com/blogs/redirecionamento-301-no-wordpress/

I understand that you want to leave domain 2.com.br as Domain in the address bar of the site that is hosted in domain 1/new. The problem is that from what I saw you are using Wordpress, the urls should not be generated correctly and the header tags of your site will not work properly. The two forms that allow it to be done as you described are with the html Frameset or iframe tags, widely used until the 2000s. Important to inform that the W3C, for example, does not recommend use of this type of resource - that is, only use it if it is the last. But if you are interested in the output, here is the documentation of Frameset: http://www.w3.org/TR/html401/present/frames.html Anyway, your SEO will be very poorly structured.

  • In fact, if by removing the WP-friendly Urls I can do that, I remove that part. I don’t mind that much. I’d really like to keep Omain. About iframe, I really don’t want to use it. I don’t even care about SEO either.

Browser other questions tagged

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