user4903
user4903

Reputation:

Use htaccess to redirect all traffic from subdomain to domain without maintaining directory structure

Most examples show how to redirect all subdomain traffic to a primary domain, maintaining the directory structure. I actually don't want this. I want to redirect all subdomain traffic (the site is going away) to the primary domain. This is not working:

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/ [R=301,L]

What happens, is if you go to this:

http://sub.newdomain.com/some/path/

You get this:

http://www.newdomain.com/some/path/

I want it all to go to the root.

Upvotes: 1

Views: 8504

Answers (3)

user2437016
user2437016

Reputation: 120

This handles wildcard subdomains, and the case where the client puts www before subdomain

#  wildcard.domain.com  ->  www.domain.com/wildcard
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteCond %1 !^(www)$ [NC]
RewriteCond %1 !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/%1/ [R=301,L]

#   www.wildcard.domain.com   ->   www.domain.com/wildcard
RewriteCond %{HTTP_HOST} ^(www\.(.*))\.domain\.com$ [NC]
RewriteCond %1 !^www$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/%2 [R=301,L]

Upvotes: 0

George Cleanthous
George Cleanthous

Reputation: 11

Just add a question mark to the end of the destination URL if you want to exclude all trailing info (eg. directory structure, parameters).

In your instance, it would simply be updated to this:

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/? [R=301,L]

Upvotes: 1

Tommy
Tommy

Reputation: 39817

How about a simple 301 redirect in the apache config file for the subdomain?

To redirect ALL files on your domain use this in your .htaccess file if you are on a unix web server:

redirectMatch 301 ^(.*)$ http://www.domain.com 
redirectMatch permanent ^(.*)$ http://www.domain.com

and another example

If you need to redirect http://mysite.com to http://www.mysite.com and you've got mod_rewrite enabled on your server you can put this in your .htaccess file:

EDIT: If you want to use this, just remove the $1 from the rules in the sample link provided if the first option above does not work.

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*)$ http://www.example.com/ [R=permanent,L]

or this:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/ [R=301,L]

These excerpts from here

Upvotes: 1

Related Questions