How can we use subdomain as part of query string?

stndn

Golden Member
Mar 10, 2001
1,886
0
0
Hello,

We are trying to figure out if there is a way we can use subdomains as part of query string that the user submitted to our page?

What that means is, supposed the user goes to our page using something like this:

URL1: one.mydomain.com/search.php?q=hello
URL2: two.mydomain.com/search.php?q=hello
URL3: six.mydomain.com/help.php

Given the three links above, they will be processed as:

URL1: search.php?q=hello&s=one
URL2: search.php?q=hello&s=two
URL3: help.php?s=six


Is this possible to accomplish? If yes, will it be with .htaccess or PHP or something else?

Sorry if it's not clear - I can try again if that's too confusing.


thanks.
-stndn.


Edited with (hopefully) better explanation

Suppose we are creating a search script, called '/search.php', located in the root www directory of our site. The search script can be used to look for houses or condos located in either san francisco, new york, or any city in the US.
The script is supposed to be accessed as follows:

www.domain.com/search.php?item=house -> search for all houses in the US
www.domain.com/search.php?item=house&city=sf -> search for all houses in SF
www.domain.com/search.php?item=condo&city=ny -> search for all condos in NY

Now, what we are trying to accomplish is to remove the 'city' as part of the query string. Instead, we want to make the city looks like a subdomain. We don't want to create real subdomains because we don't want to end up duplicating the script '/search.php' for all the city we want to support.

So now if the user wants to search, they will enter the following:

www.domain.com/search.php?item=house -> search for all houses in the US
sf.domain.com/search.php?item=house -> search for all houses in SF
ny.domain.com/search.php?item=condo -> search for all condos in NY


We want the user to only use the second set of URL and our scripts will translate them to the first set behind the scene. We don't want the second set of URL to redirect to the first set of URL.

Sorry if it's too confusing, but that's what we are trying to accomplish. Is this any better?

Btw, eventually, we *might* even attempt something like house.sf.domain.com, but that is not a priority for now...
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
13
81
www.markbetz.net
I don't think it's clear enough, to be honest. You can put anything you want into the querystring, as long as it is properly httpencoded. The question is how you want the name resolution to work, and whether there is any redirection involved. It sounds like there would have to be. So you have say, three domains that point to the same root, or you have three different roots that accept the query and redirect to a central search server to process the query? There are lots of different ways to handle these kinds of scenarios. Post a bit more specific info and maybe I can suggest an architecture.
 

stndn

Golden Member
Mar 10, 2001
1,886
0
0
Thyme: Thanks for the link. I searched for 'using subdomain as part of query string' and 'use subdomain as query string', but the results weren't helpful. We've actually used something similar to the one from the link you posted, except that we didn't detect the http/s and the port (only takes care of $_SERVER['HTTP_HOST'] and $_SERVER['REQUEST_URI'])

We'll look into the code from your link and see if we can use that. For now, it looks like we can make use of it, although we'd prefer to use .htaccess method (if any). Why is that, have to ask the manager....

Markbnj:
My apology for not making it clear. I'll try again with better example.
Suppose we are creating a search script, called '/search.php', located in the root www directory of our site. The search script can be used to look for houses or condos located in either san francisco, new york, or any city in the US.

The script is supposed to be accessed as follows:

www.domain.com/search.php?item=house -> search for all houses in the US
www.domain.com/search.php?item=house&city=sf -> search for all houses in SF
www.domain.com/search.php?item=condo&city=ny -> search for all condos in NY

Now, what we are trying to accomplish is to remove the 'city' as part of the query string. Instead, we want to make the city looks like a subdomain. We don't want to create real subdomains because we don't want to end up duplicating the script '/search.php' for all the city we want to support.

So now if the user wants to search, they will enter the following:

www.domain.com/search.php?item=house -> search for all houses in the US
sf.domain.com/search.php?item=house -> search for all houses in SF
ny.domain.com/search.php?item=condo -> search for all condos in NY


We want the user to only use the second set of URL and our scripts will translate them to the first set behind the scene. We *do not* want the second set of URL to redirect to the first set of URL (or at least, not transparent to the user).

Sorry if it's too confusing, but that's what we are trying to accomplish. Is this any better?

Btw, eventually, we *might* even attempt something like house.sf.domain.com, but that is not a priority for now...


I'll edit the original post to reflect the (hopefully) better explanation of the problem.


thanks.
-stndn.
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
Have search.php read the 'Host:' parameter sent by the client in the HTTP request. This will report www.domain.com, sf.domain.com, or ny.domain.com depending on what subdomain the user is currently on. For multilevel subdomains (e.g. house.sf.domain.com), it should be pretty self-explanatory. Just read that in and choose the course of action accordingly.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
13
81
www.markbetz.net
Yeah, as XT says, but note that you'll have to administer DNS records for all those subdomains. If you want a browser to be able to resolve ny.housesearch.com then the DNS system has to know what machine that points to. The standard way to do this is to use CNAME records to point all the subdomains to the same server, and then reroute.
 

Templeton

Senior member
Oct 9, 1999
467
0
0
you can do this with apache's mod_rewrite (assuming apache in use)
It basically uses regular expressions to rewrite url accesses. There's a demo similiar to what you want here
 

stndn

Golden Member
Mar 10, 2001
1,886
0
0
Markbnj: Don't think we have access to changing the DNS (not sure, have to ask the sysadmin on this), because we're using third party for our web hosting.

xtknight: What did you mean by reading the 'Host:' parameter sent by the client? Do you mean the http header data or something in the PHP's $_SERVER variable?

Templeton: From the demo you linked to, it seems that we'll have to make the changes to httpd.conf? If yes, then I don't think we can do that because we don't have access to that file (being on shared hosting and all)


Looks like we're limited in what we can do for now. Maybe I'll have to talk to the others and see what they say...


thanks for the help so far.
-stndn.
 

Templeton

Senior member
Oct 9, 1999
467
0
0
Originally posted by: stndn
Markbnj: Don't think we have access to changing the DNS (not sure, have to ask the sysadmin on this), because we're using third party for our web hosting.

xtknight: What did you mean by reading the 'Host:' parameter sent by the client? Do you mean the http header data or something in the PHP's $_SERVER variable?

Templeton: From the demo you linked to, it seems that we'll have to make the changes to httpd.conf? If yes, then I don't think we can do that because we don't have access to that file (being on shared hosting and all)


Looks like we're limited in what we can do for now. Maybe I'll have to talk to the others and see what they say...


thanks for the help so far.
-stndn.


You can do it in a .htaccess file as long as the host supports mod_rewrite.
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
As long as all subdomains point to the same IP you'll be fine on the DNS side of things.

Originally posted by: stndn
xtknight: What did you mean by reading the 'Host:' parameter sent by the client? Do you mean the http header data or something in the PHP's $_SERVER variable?

I mean the "Host:" parameter given in the browser's GET request to the web server. PHP should be able to read this, but I don't know off the top of my head how exactly.

It appears that it is this (you had it right before). You don't need anything more than this variable to do it properly, as far as I know.

If the HTTP_HOST header isn't set, then the $_SERVER array will lack the key (or index) HTTP_HOST, which is why you get the error. ...

So you will have to add error-checking as well. I don't know why the browser wouldn't set the Host: parameter.
 

stndn

Golden Member
Mar 10, 2001
1,886
0
0
After doing a little bit of experiment, we found two ways that we can solve the problem at hand (or at least it seems so, for now)


First solution: using .htaccess

Based on the information from the links provided above, we added the following lines in .htaccess of our www directory. See attached code, part 1.

After spending time trying to figure out why the URL keeps rewriting to www.mydomain.com (despite the lack of [..R=301..] flag), we found out that's because we asked to be redirected that way.
(smacks head)

So we modified the rewrite rule in .htaccess to the one as noted in attached code, part 2. Looking good so far


Second solution: using $_SERVER['HTTP_HOST']

We checked the contents of $_SERVER['HTTP_HOST'] and use parse_url() function to check whether the first part of the URL is www or something else. If it's not 'www', then we add the city to the $_GET value (we'll deal with city.item.mydomain.com later)

The code is attached as part 3 below. There's no error checking for missing $_SERVER['HTTP_HOST'] yet, but we'll add that later.


So far both solutions works fine in our test server, with the following tests:

http://www.mydomain.com/search.php -> gives $_GET == array ()
http://sf.mydomain.com/search.php -> gives $_GET == array ('city' => 'sf')
http://sf.mydomain.com/search.php?item=house -> gives $_GET == array ('city' => 'sf', 'item' => 'house')


We'll give it a try in our production later, to see whether there's something we missed. But for now, it seems that both solutions are up to consideration.


Btw, xtknight, if I remember correctly, the missing HTTP_HOST is usually due to the fact that
the PHP script is executed locally instead of being called via a browser. I might be wrong, though - in which case, someone with better explanation is free to correct me.


Anyways, thank you all for the help. Now to decide which method to use. Hmmm....

-stndn.
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
Cheers for posting your code. I would do htaccess if there's any doubt that HOST would work. Either way, it seems that htaccess would need to read the 'Host' parameter. How else would it know when all the subdomains (ny,sf,www) point to the same IP? I'm afraid htaccess is beyond my realm of knowledge entirely.
 

stndn

Golden Member
Mar 10, 2001
1,886
0
0
As expected, things don't go as smoothly as we first thought..

At first we thought that we were able to create subdomain and have the subdomain use the same document root as 'www'. However, after trying, we were not able to do that.

What we wanted was to have www.mydomain.com and sf.mydomain.com to point to the same document root (ie: /home/mydomain/public_html). However, there's no such option in the cpanel of our webhost to accomplish that. Instead, we were only able to have www.mydomain.com to point to ~/public_html, while sf.mydomain.com points to a subdirectory under it (~/public_html/sf)

As such, there's no way for us to share the same script across subdomains... -(


Any hints on getting all subdomains to point to the same document root? We understand that it's available through apache's 'ServerAlias' directive. But that's only available in httpd.conf, which we don't have access to....


If all else fails, we might just make it www.mydomain.com/sf/search.php?blahblahblah... -(


-stndn.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |