Forum Moderators: phranque

Message Too Old, No Replies

grab domain name with mod rewrite

         

test1

8:10 pm on Apr 9, 2008 (gmt 0)

10+ Year Member



I have a tool that allows users to create their own website online. Right now I have a bunch of rewrite rules that will take a users subdomain "user.domain.com" and change it to domain.com?user=user. I'm also trying to add the ability to allow users to attach a custom domain to their site so my basic thougth was that if users point their domains to the ip address of the server that I would use mod rewrite to check to see if the domain is not the website default domain name and if it is a custom domain, grab the domain name and pass it as a variable. Example:

www.sample.com would pass along ?domain=sample.com.

So my question is how do I only grab the domain name and use it in my rewrite rule keeping in mind that there will be more to the url then just the domain since the websites are made up of several different pages.

jdMorgan

10:14 pm on Apr 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use a RewriteCond to examine and create a back-reference to the requested hostname, then refer to it in the rule.

An example might be:


RewriteCond %{HTTP_HOST} ^(www\.)?([a-z][a-z0-9\-]*)\.example\.com

Which would allow you to refer to the username by using %2 in the RewriteRule. %1 would contain any leading "www", which would be discarded, but can be handled in in case a user thinks it is required.

Note that the pattern allows only lowercase characters, numbers, or hyphens in the subdomain, in accordance with HTTP.

Jim

test1

3:24 pm on Apr 10, 2008 (gmt 0)

10+ Year Member



Ok thanks, maybe I didn't explain clearly what I'm needing. I already have rules that grab a subdomain (although yours looks better then what I have) and pass it along as a variable so that something like username.example.com already works. But if a user wants to add a custom domain that is not a subdomain I need a way to grab the domain. So if the custom domain was like this:

www.customdomain.com I could get a variable that says domain=customdomain.com .

Does that make sense? Thanks again for the reply. The only parts I'm having a hard time with was accounting for the "www" which yours does in the example above, but then also being able to grab just the domain name if there is anything in the url after that. So I could just check for the first instance of a "\" and grab everything in between but what if there isn't a "\"? How could I account for both?

jdMorgan

4:26 pm on Apr 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



HTTP_HOST contains only the "domain" -- that is, the hostname sent by the browser, and possibly a trailing period, a colon, and a port number. So there will never be a slash in it. Something like this should work for most hostnames of the "www.example.com.:80", "www.example.co.uk:80", and "www.example.info." formats, with or without the leading "www." and/or the trailing period and port number:

RewriteCond %{HTTP_HOST} ^(www\.)?([a-z][a-z0-9\-]+\.[a-z]{2,4}(\.[a-z]{2})?)

Again, you'd use "%2" to get the "domain name" part, exclusive of any leading "www."

You will have to handle setting up the DNS to point these "custom" domains to your server's IP address, and the RewriteCond presented here is for a rule that would follow your existing "subdomain rule" in order to prevent conflicts between the two rules.

Jim

test1

4:51 pm on Apr 10, 2008 (gmt 0)

10+ Year Member



Thank you very much. It works perfectly.