SEO Friendly Wildcard DNS Set Up For Apache, IIS, Nginx

SEO Friendly Wildcard DNS Set Up For Apache, IIS, Nginx

When incorrectly set up a wildcard DNS can create what I call zombie sub-domains that can cause Google to index multiple (limitless, in fact) versions of your website; which can cause a multitude of issues for your server and your site. I wrote an article on TheSEMPost.com called “Why Google Dislikes Zombie Sub-Domains” which provides background on this issue if you would like to know more.

Here is an example of what happens when a DNS wildcard is not configured correctly for search engines… zombie sub-domains are created:

More web servers resources can be found at Apache Wildcard DNS Setup, Windows IIS Wildcard DNS Setup, Nginx Wildcard DNS Setup. This is what you want to happen if someone visits a version of your site’s URL with an incorrect subdomain:

Apache Wildcard DNS Setup

Here is how you would implement this technique in the .htaccess in the root folder of your website on an Apache-based server:

RewriteEngine on
RewriteBase /

#for all requests on xyzname.com
RewriteCond %{HTTP_HOST} xyzname\.com$ [NC]
#if they are not for the www.xyzname.com
RewriteCond %{HTTP_HOST} !^www\.xyzname\.com$ [NC]
#301 redirect to www.xyzname.com
RewriteRule (.*) http://www.xyzname.com/$1 [R=301,L]

Just replace “xyzname.com” with your own site’s domain.

Windows Server IIS Wildcard DNS Setup

And thanks to Peter Nikolow’s expertise here is how you would implement this technique in the Web config of IIS (Windows Server):

<system.webServer>
 <rewrite>
 <rules>
 <clear />
 <rule name="Redirect to WWW" enabled="true" stopProcessing="true">
 <match url=".*" />
 <conditions logicalGrouping="MatchAll">
 <add input="{HTTP_HOST}" pattern=".*" />
 </conditions>
 <action type="Redirect" url="http://www.xyzname.com/{R:0}" />
 </rule>
 </rules>
 </rewrite>
</system.webServer>

You only need to copy-paste that code into Web.config and change domain.com with your own domain.

<system.webServer>
 <rewrite>
 <rules>
 <clear />
 <rule name="RedirectToWWW" enabled="true" stopProcessing="true">
 <match url=".*" />
 <conditions>
 <add input="{HTTP_HOST}" pattern="^(?!www\.)" />
 <add input="{HTTP_HOST}" pattern="^(.+\.)?(.+\..+)$" />
 </conditions>
 <action type="Redirect" url="http://www.{C:2}/{R:0}" />
 </rule>
 </rules>
 </rewrite>
</system.webServer>

Nginx Server Wildcard DNS Setup

Here is the redirect code for Nginx, again thanks to Peter:


server {
  server_name *.example.org;
  return 301 $scheme://www.xyzname.com$request_url;
}

server {
  server_name www.xyzname.com;

  #add in further diretives to serve your content
}

When incorrectly set up a wildcard DNS can create what I call zombie sub-domains that can cause Google to index multiple (limitless, in fact) versions of your website; which can cause a multitude of issues for your server and your site. I wrote an article on TheSEMPost.com called “Why Google Dislikes Zombie Sub-Domains” which provides background on this issue if you would like to know more.

Here is an example of what happens when a DNS wildcard is not configured correctly for search engines… zombie sub-domains are created:

More web servers resources can be found at Apache Wildcard DNS Setup, Windows IIS Wildcard DNS Setup, Nginx Wildcard DNS Setup. This is what you want to happen if someone visits a version of your site’s URL with an incorrect subdomain:

Apache Wildcard DNS Setup

Here is how you would implement this technique in the .htaccess in the root folder of your website on an Apache-based server:

RewriteEngine on
RewriteBase /

#for all requests on xyzname.com
RewriteCond %{HTTP_HOST} xyzname\.com$ [NC]
#if they are not for the www.xyzname.com
RewriteCond %{HTTP_HOST} !^www\.xyzname\.com$ [NC]
#301 redirect to www.xyzname.com
RewriteRule (.*) http://www.xyzname.com/$1 [R=301,L]

Just replace “xyzname.com” with your own site’s domain.

 

Windows Server IIS Wildcard DNS Setup

And thanks to Peter Nikolow’s expertise here is how you would implement this technique in the Web config of IIS (Windows Server):

<system.webServer>
 <rewrite>
 <rules>
 <clear />
 <rule name="Redirect to WWW" enabled="true" stopProcessing="true">
 <match url=".*" />
 <conditions logicalGrouping="MatchAll">
 <add input="{HTTP_HOST}" pattern=".*" />
 </conditions>
 <action type="Redirect" url="http://www.xyzname.com/{R:0}" />
 </rule>
 </rules>
 </rewrite>
</system.webServer>

You only need to copy-paste that code into Web.config and change domain.com with your own domain.

<system.webServer>
 <rewrite>
 <rules>
 <clear />
 <rule name="RedirectToWWW" enabled="true" stopProcessing="true">
 <match url=".*" />
 <conditions>
 <add input="{HTTP_HOST}" pattern="^(?!www\.)" />
 <add input="{HTTP_HOST}" pattern="^(.+\.)?(.+\..+)$" />
 </conditions>
 <action type="Redirect" url="http://www.{C:2}/{R:0}" />
 </rule>
 </rules>
 </rewrite>
</system.webServer>

 

Nginx Server Wildcard DNS Setup

Here is the redirect code for Nginx, again thanks to Peter:


server {
  server_name *.example.org;
  return 301 $scheme://www.xyzname.com$request_url;
}

server {
  server_name www.xyzname.com;

  #add in further diretives to serve your content
}