Web.config website> Can I redirect the wrong subdomain to a specific page?

Is it possible to redirect an invalid subdomain (non-existing subdomain) to a specific page (user error page)?

EDIT: using web.config file

For example, if I typed http://foo.squishling.co.uk/ , it would redirect to something like http://squishling.co.uk/errors/incorrect_subdomain.html/ , since http: // foo. squishling.co.uk/ is not a valid subdomain.

I have not seen any code that does this, is this possible?
EDIT: If this is not possible, say so.

Thanks in advance, ~ Squishling

EDIT: if possible, a possible way to do this would be when the server receives a request for the wrong subdomain, just trick the request into requesting a page with an error

+5
source share
3 answers

Yes it is possible. Here, as with IIS 10 + UrlRewrite 2.1.

For example, suppose I have:

  • Valid domain / port good.localhost:81
  • my custom error file @ /error.txt

Step 1: Configure site bindings to accept requests for subdomains

IIS Manager β†’ website context menu β†’ "Change Bindings". Change the accepted hostname to accept *.<yourDomain> . For example: enter image description here

Detailed steps can be found on the "Substitution Header Support" documentation page.

Now the site should respond both to http://good.localhost:81 and to http://bad.localhost:81 .

Step 2: Install Url Overwrite Module

Here you can find the installer of the URL rewrite module: https://www.iis.net/downloads/microsoft/url-rewrite

Step 3: configure the redirection rule

While you can use the IIS MAnager GUI to do this, you could simply write like this to your web.config:

 <?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <rewrite> <rules> <rule name="DirectBadSubdomainsRule" stopProcessing="true"> <match url=".*" /> <conditions> <add input="{HTTP_HOST}" pattern="^good\.localhost:81" negate="true" /> </conditions> <action type="Redirect" url="http://good.localhost:81/error.txt" redirectType="Temporary" /> </rule> </rules> </rewrite> </system.webServer> </configuration> 

You can use the power of a regular expression to determine which subdomains are valid and which are not. You can also pinpoint which forwarding code you want. The above example uses a temporary redirect (HTTP307).

There is a lot of documentation here: https://www.iis.net/downloads/microsoft/url-rewrite

Testing

A bad domain should now return a redirect response:

+3
source

Sure. This is where the minimal nginx configuration is done:

 server { listen 80 default_server; server_name _; location / { rewrite ^.*$ http://squishling.co.uk/errors/incorrect_subdomain.html redirect; } } server { listen 80; server_name squishling.co.uk; location / { echo some page; } location /errors/incorrect_subdomain.html { echo incorrect subdomain error; } } 

Here's how to run it:

  • mkdir conf.d
  • put the configuration file above in conf.d/main.conf
  • docker run -p80:80 -v ~/work/test/subreq/conf.d:/etc/nginx/conf.d openresty/openresty:alpine

testing

squishling.co.uk and foo.squishling.co.uk are overridden in my /etc/hosts , so they are routed to my localhost

 curl -D - http://squishling.co.uk HTTP/1.1 200 OK Server: openresty/1.13.6.1 Date: Tue, 06 Feb 2018 18:49:25 GMT Content-Type: application/octet-stream Transfer-Encoding: chunked Connection: keep-alive some page 

Now try the wrong domain

 curl -D - http://foo.squishling.co.uk HTTP/1.1 302 Moved Temporarily Server: openresty/1.13.6.1 Date: Tue, 06 Feb 2018 18:49:34 GMT Content-Type: text/html Content-Length: 167 Connection: keep-alive Location: http://squishling.co.uk/errors/incorrect_subdomain.html <html> <head><title>302 Found</title></head> <body bgcolor="white"> <center><h1>302 Found</h1></center> <hr><center>openresty/1.13.6.1</center> </body> </html> 

Or the same after redirecting:

 curl -L -D - http://foo.squishling.co.uk HTTP/1.1 302 Moved Temporarily Server: openresty/1.13.6.1 Date: Tue, 06 Feb 2018 18:50:03 GMT Content-Type: text/html Content-Length: 167 Connection: keep-alive Location: http://squishling.co.uk/errors/incorrect_subdomain.html HTTP/1.1 200 OK Server: openresty/1.13.6.1 Date: Tue, 06 Feb 2018 18:50:03 GMT Content-Type: text/html Transfer-Encoding: chunked Connection: keep-alive incorrect subdomain error 

So, the main idea is that you define a default server configuration that will intercept all domains except those that have individual configurations. The same can be done on other web servers.

0
source

Try it. Link from fooobar.com/questions/147005 / ...

 <configuration> <system.webServer> <httpRedirect enabled="true" destination="http://squishling.co.uk/errors/" exactDestination="true" httpResponseStatus="Permanent" /> </system.webServer> <location path="incorrect_subdomain.html"> <system.webServer> <httpRedirect enabled="false" /> </system.webServer> </location> </configuration> 
-1
source

Source: https://habr.com/ru/post/1271296/


All Articles