Forum Moderators: phranque

Message Too Old, No Replies

Subdomain with many dots

mod_rewrite help when subfolders NOT required

         

craig1972

8:38 pm on Apr 30, 2007 (gmt 0)

10+ Year Member



Hello, I am looking for a solution that does redirection according to the number of dots in the subdomain, but not to subdirectories. I want them to forward to programs instead.


http://a.domain.com ==> http://domain.com/mine.php?a
http://a.b.domain.com ==> http://domain.com/mine.php?ab
http://a.b.c.domain.com ==> http://domain.com/mine.php?abc

I would appreciate any thoughts or pointers on how to accomplish this? Thanks!

jd01

12:50 pm on May 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Craig1972,

I don't normally 'just write the code', but this question isn't posted every day, and I'm not sure if my code will work, and I don't feel like testing it, so you're still kind of on your own, but I would probably start with:

# Store any 1 or more characters, not a dot, in %1, followed by a dot
# if the pattern is present 1 or more times preceding example
# followed by .com in the HTTP_HOST variable.
RewriteCond %{HTTP_HOST} ^(([^.]+)\.)+example\.com$
#Check all requests with 0 or 1 character.
RewriteRule .? http://example.com?%1 [R=301,L]

(…and then start adjusting things when it doesn't work.)

Justin

craig1972

1:37 pm on May 1, 2007 (gmt 0)

10+ Year Member



Thanks jd01! I wasn't looking for lazy coding help. Here is what I had, it is simpler, and the
(.*)
is meant to include everything that occurs before the domain name:

RewriteCond %{HTTP_HOST} ^(.*)\.example\.com$
RewriteRule .? http://example.com/url.php?u=%1 [R=301,L]

Both this, and your example, work with subdomains of one word, e.g.,

www.example.com
works. But the moment I include a subdomain with one dot, e.g.,
www.my.example.com
this gives the 500 server error.

Any thoughts or pointers would be great! So many thanks!

[EDIT]
Actually, I tested, and it only works with "www", but not with any other word before the domain, with or without the dots. What am I doing wrong? The Apache docs are a bit frustrating, and presume that I am an expert at regexp, which I unfortunately am not. If you cna point me to some regexp resource for

RewriteCond
I will happily try to help myself. Thanks!

jd01

1:45 pm on May 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could always use individual patterns:

RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule .? http://example.com?%1 [R=301,L]

RewriteCond %{HTTP_HOST} ^([^.]+)\.([^.]+)\.example\.com$
RewriteRule .? http://example.com?%1%2 [R=301,L]

RewriteCond %{HTTP_HOST} ^([^.]+)\.([^.]+)\.([^.]+)\.example\.com$
RewriteRule .? http://example.com?%1%2%3 [R=301,L]

Justin

If that doesn't work, I'm not sure, because the pattern you were using should have stored the correct information. Maybe someone else has a better guess as to where the error is coming from... My guess is it is the rule interacting in an unexpected way with another rule in the file, causing a loop.

[edited by: jd01 at 1:47 pm (utc) on May 1, 2007]

craig1972

1:45 pm on May 1, 2007 (gmt 0)

10+ Year Member



Also Justin, I am looking at this:
[webmasterworld.com...]

But it's not making much sense to me. But I'm trying.

jd01

1:49 pm on May 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just to reiterate I would have a look at your other rules.
I'm guessing that is where the issue is.
If not, I can't see it.

Justin

craig1972

1:56 pm on May 1, 2007 (gmt 0)

10+ Year Member



Thanks, but I don't have any other rules. Actually the only line I have above this is

Options +FollowSymlinks -Indexes

jd01

1:59 pm on May 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Interesting…

Try adding to the rule/rules:
# Make sure the HOST header is set, and the HOST is not example.com.
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^example\.com$
# Above should be added to all rules for this set if more than 1
RewriteCond %{HTTP_HOST} ^([^.]+)\.([^.]+)\.([^.]+)\.example\.com$
RewriteRule .? http://example.com?%1%2%3 [R=301,L]

Justin

Make sure you add the space back between the } and the ! if you copied and pasted before I posted this correction.

craig1972

2:14 pm on May 1, 2007 (gmt 0)

10+ Year Member



Thanks for the help. Not working. So I am trying to simply hard code one example to see if it works, and it doesn't :(

Here is my entire .htaccess:


Options +FollowSymlinks -Indexes
RewriteEngine On
RewriteCond %{HTTP_HOST} ^my\.only\.subdomain\.example\.com$
RewriteRule .? http://example.com/url.php?u=my.only.subdomain [R=301,L]

Any ideas why even this pretty straightfwd hardcoded example won't work?

jd01

2:35 pm on May 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, sorry.

Maybe someone else will have some thoughts.

Justin

jdMorgan

2:41 pm on May 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How, specifically, does it "not work"?

How do you test?
- With what URL?
What is the result?
- Do you get a browser error message?
-- If so, what is it?
--- If it's "Internet Explorer cannot display the webpage" or "Server not found," do you have your DNS set up for wild-card subdomains?
- Do you get a server error?
-- If a server error, what is in the server error log?
How does this result differ from what you expect?

Jim

jd01

2:46 pm on May 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



craig1972: I didn't see the edit to your 2nd post.
My guess is your server is only setup to work with www. and example.com.

jdMorgan: He's getting a 500 error when attempting to redirect with more than one sub. in the URL, but www. works correctly.

ADDED: I think you got it right here...

do you have your DNS set up for wild-card subdomains?

Glad you were awake.
I missed it.

Justin

jdMorgan

3:05 pm on May 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The list above is a general procedure.

The question that applies in this specific case with a server error is: "What is in the server error log?"

If there was a DNS problem, then the request would never arrive at the server, so you wouldn't get a 500 error. I suppose it's still possible that the server itself is not set up to recognize the subdomains, but it's more likely some other problem.

Nothing here rules out a problem with the script itself, either. We need to see that error log info...

Jim

craig1972

3:13 pm on May 1, 2007 (gmt 0)

10+ Year Member



Thanks Justin, and JD. The script is fine. The script never reaches a 500. I just get the "Server not found". How can I setup Apache for wildcard DNS?

jdMorgan

3:34 pm on May 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is a two-part problem: First, you must edit your DNS zone file to point all subdomains to your server's IP address. Then you must configure your server to recognize those subdomains.

How you set up the DNS zone file --or whether it's even possible-- depends on who owns your domain servers, and what kind of control panel they provide for doing this.

How you set up the server to recognize wild-card subdomains --or whether it's permitted-- depends on your hosting company and what level of access they grant you to the server configuration. The higher-grade hosting plan you have, the more likely that you'll be able to do this. In fact, some hosts configure the server to recognize all subdomains by default, especially if you have a static IP address.

Sorry for all the complications, but there is no simple answer...

Jim

craig1972

4:00 pm on May 1, 2007 (gmt 0)

10+ Year Member



Thanks Jim. I have a fully dedicated server, with 5 IPs. I use WHM/Cpanel. Subdomains work from cpanel. But using cpanel, I cannot put in subdomains that have dots in them.

My server is unmanaged however, so I did a google and found that the wildcard subdomain thing is as simple as putting


ServerAlias *.domain.tld

This I did, then restarted Apache, and tested. The main domains (with and without "www") work. Now what can I do?

jdMorgan

4:37 pm on May 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As described above, set up your DNS to support the wild-card subdomains. It's no harder than what you've just done: Just add an "A" record pointing "*.example.com." (note the trailing period) to your IP address.

Depending on your DNS control panel, it might look something like:


*.example.com. 86400 IN A 192.168.0.1

Just use the existing info you see for "www.example.com" as a template. "86400" is the "time-to-live" (in seconds) that indicates how long this data can be cached by other DNS servers.

Jim

craig1972

4:52 pm on May 1, 2007 (gmt 0)

10+ Year Member



Thanks. This done too. From the "Edit DNS Zone".

Still [random.mydomain.com...] does not work. The [mydomain.com...] works. Only "www" works. The .htaccess has no instructions for subdomains anymore.

jdMorgan

6:17 pm on May 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It will take four hours to several days for your DNS changes to propagate and become consistently-available.

Jim