Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redirect all wildcard subdomains to root domain

Okay, I have done a fair amount of searching but still can't find something specific enough to handle my problem.

Right now I have a DNS record that redirects all other subdomains to my server. What I'm asking is what would make example.mydomain.com return HTTP/1.1 301 and redirect to just mydomain.com

My apologies if this has been covered, I just couldn't find anything specific enough.

like image 496
nkcmr Avatar asked Jul 09 '12 02:07

nkcmr


People also ask

How do I set up a wildcard redirect for a subdomain?

Select Domain List from the left sidebar and click on the Manage button next to your domain: 3. Find the Redirect Domain section and click on the Add Wildcard Redirect button: 4. Fill in the Destination URL with a link you would like to redirect your subdomain to and check Save Changes: The catch-all feature can be set up for all types of records.

What is the difference between a wildcard and a subdomain?

Also, if a separate subdomain is created, it has a higher priority than wildcard. If your domain is pointed to our BasicDNS, PremiumDNS or FreeDNS, this feature is available for you. A wildcard record can be set up for the following record types: A, AAAA, ALIAS, CNAME, TXT, URL Redirect, MX.

How do I redirect my subdomain to another DNS record?

Fill in the Destination URL with a link you would like to redirect your subdomain to and check Save Changes: The catch-all feature can be set up for all types of records. In order to select another record type go to the Advanced DNS tab: - Find the Host records section and click on the Add New Record button ( not able to edit Host Records? ):

Can I create a catch-all (wildcard) record for a subdomain?

If you create a separate subdomain, it will take precedence over your wildcard. Catch-all (wildcard) records will only work for A, CNAME, URL redirect, AAA, TXT, and MX record types. They do not work with NS records.


1 Answers

Use a RewriteCond to match domains other than mydomain.com and 301 redirect them:

RewriteEngine On
# If the domain (any domain) is not exactly mydomain.com...
RewriteCond %{HTTP_HOST} !^mydomain\.com$ [NC]
RewriteRule (.*) http://mydomain.com/$1 [L,R=301,QSA]

You can also look more specifically for subdomains of mydomain.com. The one above would match any other domain.

# Match only subdomains of mydomain.com
RewriteCond %{HTTP_HOST} ^(.+)\.mydomain\.com$ [NC]
RewriteRule (.*) http://mydomain.com/$1 [L,R=301,QSA]
like image 171
Michael Berkowski Avatar answered Nov 07 '22 04:11

Michael Berkowski