How to redirect a page using . htaccess?

Asked

Viewed 129 times

3

how have you been? Well, I’ve been facing a problem that boils down to the following:

I want through the file .htaccess redirect only the home page from my website to another domain. Example:

Redirect: www.site1.com.br -> www.site2.com.br

If you have other pages in the same domain (Example: www.site1.com.br/algumacoisa), should not redirect for the second domain (www.site2.com.br).

  • I find it easier to do this with php

  • And how could I do that using php? Could give me a light?

  • I was wrong about the code, I made an edit

1 answer

3


With php you can put in your index as follows:

$siteAtual = str_replace(["www.", "http://", "https://"], "", $_SERVER['HTTP_HOST']);
if($siteAtual == "meusite.com" && $_SERVER['REQUEST_URI'] == "/"){
    header("Location: http://www.novosite.com.br");
}
  • $_SERVER['HTTP_HOST'] - takes the main URL
  • $_SERVER['REQUEST_URI'] - take what you have after the main URL

If what you have is empty, redirect the citizen.

  • 1

    That’s exactly what I need, thank you! ☺

Browser other questions tagged

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