H. Ferrence
H. Ferrence

Reputation: 8106

How to Redirect to a Different Domain

I need to setup a directive in my .htaccess file to redirect as follows:

from http://mydomain.com/internal/

to http://myotherdomain.com/internal/

Can anyone assist?

Thanks

MY CODE -- PRODUCES 500 INTERNAL SERVER ERROR

//Rewrite to www
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain.com/internal[nc]
RewriteRule ^(.*)$ http://myotherdomain.com/internal/$1 [r=301,nc]

Upvotes: 0

Views: 251

Answers (2)

Christopher
Christopher

Reputation: 2057

Try this:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^mydomain.com$ [NC]
RewriteRule ^internal(.*)$ http://myotherdomain.com/internal$1 [L,NC]

The (.*) will copy anything so you can use it in $1. If the redirect should be permanent, just add R=301 after L,NC

Edit Your mistakes in the given piece of codes are:

  1. comments use "#"
  2. The host is ^mydomain.com$
  3. You have to add a space between the host and [NC]
  4. Write RewriteRule ^(.*)$ http://myotherdomain.com/$1 OR RewriteRule ^internal/(.*)$ http://myotherdomain.com/internal/$1, but your Rule will redirect to internal/internal

Upvotes: 1

Ansari
Ansari

Reputation: 8218

You had a few issues with your file: Comments start with # not //, you can't match the URI with HTTP_HOST (you were trying to match /internal), and there needs to be a space between the rule or cond and the flags (NC). This should work though:

#Rewrite to www
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain.com$ [NC]
RewriteRule ^internal(.*)$ http://myotherdomain.com/internal$1 [R=301,NC]

Upvotes: 2

Related Questions