Forum Moderators: phranque

Message Too Old, No Replies

Need help redirecting old site to new one

         

rlopes

1:29 pm on Jan 28, 2022 (gmt 0)

10+ Year Member Top Contributors Of The Month



My old website is example.com.br
I need to redirect it to example.com
I am using htaccess to achieve that, but I am getting a 404 error.
One more detail is that we need to redirect the www version to the non-www one.

Does anybody know how to write these rules?

w3dk

3:00 pm on Jan 28, 2022 (gmt 0)

10+ Year Member Top Contributors Of The Month



Please show what you have tried and the contents of your htaccess file. Are you getting any kind of redirect? Or does nothing appear to be happening? Are the old and new domains pointing to the same place? Or is the old domain pointing to a different server?

rlopes

4:22 pm on Jan 28, 2022 (gmt 0)

10+ Year Member Top Contributors Of The Month



<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^example.com.br$ [OR]
RewriteCond %{HTTP_HOST} ^example-ii.com.br$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

</IfModule>


[edited by: not2easy at 6:21 pm (utc) on Jan 28, 2022]
[edit reason] ummm, please use example.com [/edit]

rlopes

4:24 pm on Jan 28, 2022 (gmt 0)

10+ Year Member Top Contributors Of The Month



That's what I have in the file at the moment.

lucy24

6:19 pm on Jan 28, 2022 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Someone will come along and change the code to example.com, example.old, example.br and so on, so we can see what you're saying.

For now: Do both sites live on the same server, subject to the same htaccess? Are they the only sites on the server?

Where is this pair of redirects located, relative to other RewriteRules in the same htaccess? They should be at the very end.

The <IfModule> envelope isn't necessary. It does no harm; it's just clutter.

The first rule isn't actually necessary, assuming there's nothing else on the server except Old Site and New Site. If so, the whole thing can be handled with a single canonicalization redirect, best expressed as a negative:
RewriteCond %{HTTP_HOST} !^example\.new$
RewriteRule (.*) http://example.new/$1 [R=301,L]

If you're concurrently moving to https--which you really should be doing at this point--simply add another RewriteCond before the existing one, like this:
RewriteCond %{HTTPS} !on [OR]

If there are other sites on the same server, subject to the same htaccess, that are not involved in the redirect, the rules become a little trickier. And if the various hostnames live on different servers, the rules will again look different.

w3dk

6:53 pm on Jan 28, 2022 (gmt 0)

10+ Year Member Top Contributors Of The Month



Where is this pair of redirects located, relative to other RewriteRules in the same htaccess? They should be at the very end.


Don't you mean at the very start?

rlopes

7:37 pm on Jan 28, 2022 (gmt 0)

10+ Year Member Top Contributors Of The Month



My website's URL is: https://example.com. I also have the .com.br variant.
I have made it so that if anyone tries to access the website using the .com.br domain, it redirects them to the .com one.

However, if one tries to access it using https, it won't redirect them at all.
It only works if they use http, i.e.: http://example.com.br/, then it will redirect them to https://example.com/

How can I fix that?




[edited by: not2easy at 8:19 pm (utc) on Jan 28, 2022]
[edit reason] Please see TOS [webmasterworld.com] [/edit]

lucy24

8:12 pm on Jan 28, 2022 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Don't you mean at the very start?
No, because this is essentially a canonicalization redirect. As such it should come after any rules that apply to specific URLs--and, of course, after any access-control rules made with mod_rewrite.

But I should have said: after all external redirects. Internal rewrites ([L] flag alone), including anything done by a CMS, come after those. (And rules with no [L] flag, such as setting cookies, come last of all--but those are pretty rare.)

lucy24

8:14 pm on Jan 28, 2022 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



However, if one tries to access it using https, it won't redirect them at all.
Uh-oh. Better explain some more about how your site handles HTTPS. Sometimes there's a third-party involvement and then all the rules change.

:: looking vaguely around for phranque, who can explain it coherently ::

Edit: rlopes, there are two reasons you're not allowed to use your actual site name in these forums. One of the two reasons applies especially to the Apache subforum: everything turns into a link, and then people can no longer see what you meant to post. For this reason, change your real sitename to example.com. In fact you can use any TLD: example.org, example.net, example.br, example.old, example.new and so on.

Sometimes it also helps to put things inside [ code ] tags, especially when quoting htaccess rules.

not2easy

8:22 pm on Jan 28, 2022 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



However, if one tries to access it using https, it won't redirect them at all.
It only works if they use http, i.e.: http://example.com.br/

Is this by any chance a wordpress site where the URL is controlled by settings?

BTW - thank you lucy24 for the explanation about why we use example.com for domains at WebmasterWorld. I'm sure that the domain ad earlier was a surprise, but that can happen with any substitute domain except example.com - no one can buy that domain.

w3dk

11:58 pm on Jan 28, 2022 (gmt 0)

10+ Year Member Top Contributors Of The Month



...I should have said: after all external redirects. Internal rewrites ([L] flag alone), including anything done by a CMS, come after those.


Right, agreed. I was going to say... putting the canonical redirects literally at the "very end" is probably the number one cause of error I see in the wild. And then wondering why it's either not doing anything or redirecting to the front-controller. Whilst putting the canonical redirect(s) at the very start may not be the most optimal; it should still "work".

w3dk

12:34 am on Jan 29, 2022 (gmt 0)

10+ Year Member Top Contributors Of The Month



My website's URL is: https://example.com.


So, why are you redirecting to HTTP in the rules above? (Is it getting redirected (again) to HTTPS later?)

However, if one tries to access it using https, it won't redirect them at all.
It only works if they use http, i.e.: http://example.com.br/, then it will redirect them to https://example.com/


To clarify, you do still have SSL certs installed for the other domains? And the server is accepting secure requests to these other domains?

w3dk

1:15 am on Jan 29, 2022 (gmt 0)

10+ Year Member Top Contributors Of The Month



...but I am getting a 404 error.


But what's triggering the 404? Is this an Apache generated 404 or a 404 from your website/application?

Are the URLs at old-site and new-site the same? You have only changed the domain?