How to redirect subdomains that do not exist?

I am trying to redirect using .htaccess as follows. I am not so familiar with .htaccess, so I'm not sure if this can be done. Also, I don’t know how I intend to do this, following the best SEO techniques.

www.domain.com                > domain.com 301

ks.domain.com                 > kansas.domain.com 301

ia.domain.com                 > iowa.domain.com 301

domain.com/sites              > domain.com 301

domain.com/sites/iowa         > iowa.domain.com 301

nonexistent.domain.com        > domain.com 302

domain.com/sites/nonexistent  > domain.com 302

My biggest question is whether I can detect a non-existent subdomain and redirect it. I would like to see how all this is done.

+5
source share
2 answers

First, you need to add wildcard subdomains by creating a subdomain with *as its name only if your web host allows you to do this. And this should be in yours .htaccess, try checking it out if it works:

Options +FollowSymlinks
RewriteEngine on

RewriteCond %{HTTP_HOST} ^www\.domain\.com
RewriteRule ^(.*)$ http://domain.com/$1 [R=301]

RewriteCond %{HTTP_HOST} ^ks\.domain\.com
RewriteRule ^(.*)$ http://kansas.domain.com/$1 [R=301]

RewriteCond %{HTTP_HOST} ^ia\.domain\.com
RewriteRule ^(.*)$ http://iowa.domain.com/$1 [R=301]

RewriteCond %{HTTP_HOST} ^domain\.com
RewriteCond %{REQUEST_URI} ^/sites/?$
RewriteRule ^(.*) / [R=301]

RewriteCond %{HTTP_HOST} ^domain\.com
RewriteCond %{REQUEST_URI} ^/sites/iowa/?$
RewriteRule ^(.*) http://iowa.domain.com/ [R=301]

RewriteCond %{HTTP_HOST} ([a-z0-9-]+)\.domain\.com$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) http://domain.com/ [R=302]

RewriteCond %{HTTP_HOST} ^domain\.com
RewriteCond %{REQUEST_URI} ^/sites/([a-z0-9-_]+)/?
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*) http://domain.com/ [R=302]

-f, , , -s, , 0 -d, , .

+7

, , , .

, , metaa.stackoverlow.com, : *.stackoverflow.com. cpanel , " "? . .htaccess, * .

, :

subdomain.site.com/*

*.site.com/dir

*.site.com/*

0

All Articles