Forum Moderators: phranque

Message Too Old, No Replies

Redirect non-existing subdomains to main domain

mode rewrite

         

ldor

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

5+ Year Member



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?

not2easy

7:13 pm on Sep 12, 2019 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



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.

ldor

7:39 pm on Sep 12, 2019 (gmt 0)

5+ Year Member



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]

tangor

9:42 pm on Sep 12, 2019 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



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.

penders

10:17 pm on Sep 12, 2019 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



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?

phranque

10:58 pm on Sep 12, 2019 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



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)

ldor

9:17 am on Sep 13, 2019 (gmt 0)

5+ Year Member



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

phranque

9:39 am on Sep 13, 2019 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



what did you end up using?

ldor

10:35 am on Sep 13, 2019 (gmt 0)

5+ Year Member



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

lucy24

12:53 am on Sep 14, 2019 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



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.