Link to home
Start Free TrialLog in
Avatar of Dido123
Dido123

asked on

Redirecting my website to another url

Hi,

I want to redirect my domain to another url. I want any request will be made on my domain to be redirected. EX:

www.website1.com redirects to www.website2.com/mypage/
website1.com redirects to www.website2.com/mypage/
www.website1.com/anything/1.html redirects to www.website2.com/mypage/
website1.com/anything/ redirects to www.website2.com/mypage/

I hope you got what I meant. Currently website1.com is hosted on my server.


Thank you so much for the help.
Avatar of mziter
mziter

Most of the time you can set that up through your hosting company/control panel.

Also, you can use code on the actual page. What code are the pages written in?

You can put the code below in between your head tags (<head> PUT HERE </head>) on the pages you want redirected also.
<meta HTTP-EQUIV="REFRESH" content="0; url=http://www.website2.com/mypage">

Open in new window

You could put a index.php on the root of website1.com
and place in it
<?php
header('Location:http://www.wwebsite2.com/mypage/');
?>

same in website1.com/anything/
but alter the Location for your needs.
Use below code in your default php file
Remove other tags
<html>
<?php
   header( 'Location: http://www.newwebsite.com/mainpage.html' ) ;
?>
</html>
Avatar of Dido123

ASKER

I know of all these suggestions. The only problem with these suggestions is if someone hits www.website1.com/anypage/ or website1.com/oldpage.html instead of www.website1.com/index.html he will hit 404 page.

website1.com used to be a website so it has several old pages. All what I want is to redirect any request will website1.com to website2.com/mypage/ ... I'm pretty sure .htaccess file will be the option but I don't know what I should write in it.

Write below code in htaaccess file, it will redirect all to example.com, whatever the traffic is
Redirect 301 / http://www.website2.com/
Write this in your .htaccess file of website1


RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^website1.com [NC]
RewriteRule ^(.*)$ http://website2.com/anypage$1 [L,R=301]


Hope this helps
Addy...
Avatar of Dido123

ASKER

@abatin your line is working fine but there is a problem.

Let's say the line is : Redirect 301 / http://www.website2.com/yes/

When I request www.website1.com/no123/ it redirects to http://www.website2.com/yes/no123/

I want it to redirect to http://www.website2.com/yes/ no matter what I requested.
Have u tries  my code in .htaccess file????


Addy
Avatar of Dido123

ASKER

Yes Addy, but it always redirects to http://website2.com

I tried to set it as this but it didn't work :

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^website1.com [NC]
RewriteRule ^(.*)$ http://website2.com/realpage/$1 [L,R=301]

also

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^website1.com [NC]
RewriteRule ^(.*)$ http://website2.com/realpage$1 [L,R=301]


Both didn't redirect to http://website2.com/realpage/ or http://website2.com/realpage as I wanted.

I have no problem trying it again if you can solve this problem.

Thank you both for the help.
Try this:


RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.website1.com [NC]
RewriteRule ^(.*)$ http://website2.com/realpage$1 [L,R=301]


Hope this helps,
Addy
Avatar of Dido123

ASKER

Addy, I think you misunderstood me. I didn't mean I have a www and non-www problem.

I mean it doesn't redirect to the correct directory.

The link I want to redirect to is : www.website2.com/newpage/

All what I want is, If any visitor no matter what he writes he will be redirected to www.website2.com/newpage/

EX: If he visited,

website1.com
www.website1.com
website1.com/oldiee/
website1.com/old-index.html
Whatever etc...

He will be redirected to www.website2.com/newpage/

Your current code only redirects to www.website2.com ... not including the directory /newpage/

I hope my question is more clear now ...

Thank you so much for your time.
ok
Try this:


RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.website1.com [NC]
RewriteRule ^(.*)$ http://website2.com/newpage [L,R=301]


Hope this helps,
Addy
Avatar of Dido123

ASKER

OK .. now we're talking! :)

It's exactly like I want it. One more question.

I had to duplicate your code so I can assure it's working with both www or non-www

Because If I did like this :

RewriteCond %{HTTP_HOST} ^www.website1.com [NC]

non-www wouldn't redirect and vice-versa. I duplicated the code for non-www but I thought there might be one code that will work with both www and non-www

Addy, the 500 points is defiantly yours.
ASKER CERTIFIED SOLUTION
Avatar of Avinash Zala
Avinash Zala
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dido123

ASKER

Awesome .. it works! :)