Hosting & Domaining Forum

Hosting & Domaining development => Programming Discussion => Topic started by: autorenta on Aug 08, 2022, 04:47 AM

Title: Redirect from all non-existent subdomains to the main domain site
Post by: autorenta on Aug 08, 2022, 04:47 AM
Hello, can you tell me how to set up redirects from all non-existent subdomains to the main domain?
That is, when going to the example using the link dsdsfdsf.somesite.com, it was transferred to somesite.com

I set up A record like *.somesite.com 1.1.1.1, if before that, when going to dsdsfdsf.somesite.com and so on, nothing opened at all, now it throws a 404 error, which is not written in .htaccess, but just a system one.
I didn't do anything in the .htaccess file, I tried all the options that I found, as if subdomains didn't react to it at all.
Title: Re: Redirect from all non-existent subdomains to the main domain site
Post by: alex.thomson on Aug 08, 2022, 05:05 AM
So you told the browser (and other "clients") to apply "for subdomains" to the server with IP = 1.1.1.1
For right operation, the web server must also be configured. All subdomains need to be referred to one folder. If this is some kind of arbitrary folder, it will be enough to add an unconditional redirect to the main domain in the config. If for some reason a root document from the main domain is specified, something like that:

RewriteCond %{HTTP_HOST} \.site\.name$
RewriteRule (.*) https://site.name [R=301,L]

These lines need to be placed above the rest of the internal rules, you can immediately after RewriteEngine On
Title: Re: Redirect from all non-existent subdomains to the main domain site
Post by: _XyJIuGaN_ on Oct 13, 2022, 12:36 PM
Add an A record to the domain's DNS records, f.e.  *.example.com . A 10.0.0.1
Where example.com is your domain, 10.0.0.1 is the IP address your domain links to. support of the hosting provider whose services you use.

open the file. htaccess, which must be located in website directory, write

RewriteEngine On
RewriteCond %{HTTP_HOST} !^example\.com$
RewriteRule ^(.*)$ http://example.com/$1

Where example.com is your domain