Welcome to WebmasterWorld Guest from 34.204.203.142

Forum Moderators: Ocean10000 & phranque

Redirect non-existing subdomains to main domain

mode rewrite

     
6:02 pm on Sep 12, 2019 (gmt 0)

New User

joined:Sept 12, 2019
posts:9
votes: 1


Hi,
I need to redirect some non-existing subdomains on my site to www. (actually, I need to redirect some specific domains but a solution for all non-existing subdomains would also be all right). I've found a lot of solutions using mode rewrite out there on the web but none of them works for my site. Could there be something in the site configuration that prevents those solutions from working?
7:13 pm on Sept 12, 2019 (gmt 0)

Administrator from US 

WebmasterWorld Administrator not2easy is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Dec 27, 2006
posts:4558
votes: 363


Hi idor and Welcome to WebmasterWorld [webmasterworld.com]

It would help here if we could see what you had tried that didn't work and if we were sure of what "didn't work" meant. In case you missed the Charter ( and/or ToS) here, please replace your actual domain name with 'example.com when you post code as we don't want to discuss actual htaccess issues on known domains.
7:39 pm on Sept 12, 2019 (gmt 0)

New User

joined:Sept 12, 2019
posts:9
votes: 1


Hi,
I've deleted some of the solutions that did not work but I still have three solutions which I've commented out but kept in the .htaccess file (all of them found in various forums). An of course, there is RewriteEngine On before each of them:

1.
RewriteCond %{HTTP_HOST} ^mysite.org [NC]
RewriteRule ^(.*)$ http://www.mysite.org/$1 [L,R=301]


2. (t. in this solution is the subdomain that does not exist anymore)
RewriteCond %{HTTP_HOST} ^t.mysite$ [NC]
RewriteRule (.*) http://www.mysite.org/$1 [R=301,L]


3.
RewriteCond %{HTTP_HOST} !^mysite\.org$ [NC]
RewriteCond %{HTTP_HOST} !^(valid1|valid2|valid3)\mysite\.org$
RewriteRule ^ http://www.mysite.org%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^ http://www.mysite.org%{REQUEST_URI} [R=301,L,NE]
9:42 pm on Sept 12, 2019 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member tangor is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Nov 29, 2005
posts:10570
votes: 1124


Just asking ... are you hoping to turn hacker attempts into a hit instead of returning a 404 for something that does not exist?

Probably won't give you to the result you seek.
10:17 pm on Sept 12, 2019 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3153
votes: 7


What do you mean exactly by "non-existing" subdomains?

What response are you getting when you request one of these "non-existent" subdomains?

Obviously the subdomain must physically "exist" (ie. the relevant DNS entries are in place and your server is configured to accept requests to this subdomain) otherwise the request won't actually reach your server and the redirect directives won't be seen.


RewriteCond %{HTTP_HOST} ^mysite.org [NC]
RewriteRule ^(.*)$ http://www.mysite.org/$1 [L,R=301]


So, even this does nothing? Or do you get an error when requesting the domain apex?

Also, test with 302 (temporary) redirects and make sure you've cleared your browser cache.

Do you have other directives in your .htaccess file?
10:58 pm on Sept 12, 2019 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11872
votes: 245


welcome to WebmasterWorld, Idor!

i'm taking some guesses here but i think #3 is the closest to a solution.
however you need to combine the two rulesets to avoid chained redirects and make some other modifications for it to work properly.

this might be close to what you need:
RewriteCond %{HTTPS} on [OR]
RewriteCond %{HTTP_HOST} !^((www|valid1|valid2|valid3)\.example\.org)?$ [NC]
RewriteRule (.*) http://www.example.org/$1 [R=301,L]


notes:
- if your canonical protocol is http: then you should be testing for https: to fire the redirect
- the second RewriteCond will fire the redirect if the requested hostname isn't exactly www.example.org or valid1.example.org or valid2.example.org or valid3.example.org
- you don't need the [NE] (no escape) flag unless you have reserved characters in your RewriteRule target.
(see: https://tools.ietf.org/html/rfc3986#section-2.2 )

- this is a catch-all redirect.
you will need to precede this ruleset with specific rulesets to handle noncanonical requests to the non-www hostnames.
for example, you need a ruleset to redirect https:valid1.example.org to http:valid1.example.org (et al)
9:17 am on Sept 13, 2019 (gmt 0)

New User

joined:Sept 12, 2019
posts:9
votes: 1


Than you everyone for your suggestions. In the end penders' suggestion has helped. I do not know much about the proper use of .htaccess, I did not know that the subdomains must actually exist. I've re-created those subdomains that I do not need anymore and then the redirect started working
9:39 am on Sept 13, 2019 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11872
votes: 245


what did you end up using?
10:35 am on Sept 13, 2019 (gmt 0)

New User

joined:Sept 12, 2019
posts:9
votes: 1


RewriteEngine on
RewriteCond %{HTTP_HOST} ^m.mysite.org [NC]
RewriteRule ^(.*)$ http://www.mysite.org/$1 [L,R=301]


(m. is the subdomain I need to redirect).
But as I said, it appeared that the subdomain should exist otherwise this code does not work
12:53 am on Sept 14, 2019 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15934
votes: 889


it appeared that the subdomain should exist
Well, there are different levels of existence.* It has to “exist” in the sense that there has to be a DNS that points requests to some location where they will meet your htaccess file. This part might be either wild-card subdomains or something more specific, depending on your setup. But it doesn’t have to “exist” in the sense of having any physical files on your server.


* For example, for the last 9 days my computer simultaneously did and did not exist, because it was in the shop.