Abdul Rauf
Abdul Rauf

Reputation: 761

Redirect url from one domain to another using htaccess

I have sub domain and main domain. I want to redirect all the url of subdomain to their domain. like if user will write

1) abc.example.com   then it should redirect to www.example.com
2) abc.example.com/mno/jkl_123.html then it should redirect to www.example.com/mno/jkl_123.html

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

And same as for other URL's of site. I have tried following rule in htaccess.

it is working for for images or point 1 type of URL which is defined above. But not working for Point 2.

Can anybody let me know what i need to change to make it work.

Upvotes: 0

Views: 517

Answers (1)

arkascha
arkascha

Reputation: 42984

I made some small modifications:

<IfModule mod_rewrite.c>
  RewriteCond %{HTTP_HOST} ^abc\.example\.com$ [OR]
  RewriteCond %{HTTP_HOST} ^www\.abc\.example\.com$
  RewriteRule ^/?(.*)$ http://www.example.com/$1 [R=301,L,QSA]
</IfModule>

Provided you placed your .htaccess style file in the correct location and it's interpretation is enabled at all in your http host configuration then this should work.

Also you need to have the host abc.example.com and www.abc.example.com defined and pointing to that location in the file system, or they have to be served by the default host inside your http server.


And a general hint: you should always prefer to place such rules inside the http servers host configuration instead of using .htaccess style files. Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).

Upvotes: 1

Related Questions