Forum Moderators: phranque

Message Too Old, No Replies

Rewrite subdomain into php variable?

Tried a whole mess of stuff

         

lucas20

9:12 pm on May 27, 2008 (gmt 0)

10+ Year Member



Does anyone know how to translate

[sub.example.com...]

into

http://example.com/query.php?q=sub

using mod rewrite? is this possible?

jdMorgan

9:24 pm on May 27, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This recent thread [webmasterworld.com] may help you get started.

Please see the references cited in our Forum Charter for more information.

Jim

lucas20

10:08 pm on May 27, 2008 (gmt 0)

10+ Year Member



well, yes it is similar, but I cannot get it functioning, just 404s.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^([a-z]{2})\.example\.com
RewriteRule ^([a-z_]+)/?$ /query.php?q=$1 [L]

jdMorgan

10:20 pm on May 27, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There are no cut-and-paste solutions. You will need to adapt those regular-expressions patterns to suit your application.

If after reading our Forum Charter [webmasterworld.com], you have a specific question that comports with our purpose here, feel free to ask.

Jim

lucas20

2:06 am on May 28, 2008 (gmt 0)

10+ Year Member



Question: Does anyone know why this is not working?

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com
RewriteRule ^(.*)\.example\.com /query.php?q=$1 [L]

to turn [sub.example.com...]

into

http://example.com/query.php?q=sub

jdMorgan

12:25 pm on May 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It doesn't work because RewriteRule sees only the requested local URL-path, and not the hostname/domain. Therefore, the rule will never match, unless you request a URL such as "xyz.example.com/en.example.com/". And even then, the "q=" value would be taken from the URL-path ("en"), and not from the subdomain ("xyz") as you desire.

This brings up another question, though. What do you intend to do if a "page" is specified in the URL? Do you want to skip the rule, move the page-path into a second php variable, or do something else with it?

Putting that off for the moment, the following change will make your rule work, but only for the home page of any given subdomain:


Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule ^$ /query.php?q[b]=%1[/b] [L]

Note that the back-reference in the substitution changed from "$1" to "%1" so that the value is taken from the RewriteCond match, and not from the RewriteRule.

You may also want to exclude the "www" subdomain from being rewritten to your script -- That's up to you to decide.

Jim

lucas20

4:52 pm on May 28, 2008 (gmt 0)

10+ Year Member



Fantastic - that definitely helps to clarify!

This works, now, but only for www.example.com which sends me to http://example.com/query.php?q=www - but does not for say foo.example.com.

I was hoping to add a rule (below) so that it does not enact the rule for specific pages OR for 'www'

RewriteCond %{HTTP_REFERER} !^http://(www\.)?example.com/.*$ [NC]

jdMorgan

9:33 pm on May 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To exclude "www", add another RewriteCond:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule ^$ /query.php?q=%1 [L]

This code will rewrite all requests for first-level subdomains (except www.example.com) such as foo.example.com and bar.example.com, but not www.foo.example.com, to query.php. But the requests must arrive at your server (i.e DNS must be configured), and must result in the execution of this code (i.e. server must be configured) in order for this code to have any effect.

You didn't say specifically what the results were when you tested "foo.example.com", and "it doesn't work" is not useful for diagnosing a problem, but since it's possible that you received a domain-resolution-related error, the following points should be investigated:

1) Do you have an "A record" in your DNS zone file set up to point wildcard subdomains to your server's IP address?

2) Does your server have a unique (non-shared) IP address? (If not you'll need to add each subdomain using whatever "control panel" your host provided, and you may not be able to use the code method we're discussing here.)

Jim

lucas20

2:07 am on Jun 3, 2008 (gmt 0)

10+ Year Member



Using the code above, I get a "missing input" - or that is to say, it is sending null to the script.

lucas20

2:08 am on Jun 3, 2008 (gmt 0)

10+ Year Member



can't edit, but this works fine now

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule ^$ /query.php?q=%1 [L]

Placed onto a unique IP with a hosting who was willing to add a wildcard subdomain.

[edited by: jdMorgan at 5:31 pm (utc) on June 3, 2008]
[edit reason] Snipped hosting company name. See TOS. [/edit]

lucas20

9:52 pm on Jun 3, 2008 (gmt 0)

10+ Year Member



pm me if you want the host - it took me a few to find one that i could work with.

jdMorgan

2:44 am on Jun 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you have a unique IP address and true IP-based virtual/shared hosting (as opposed to name-based virtual/shared hosting), then *you* should be able to control the DNS, and should be able to add as many subdomains, or indeed, as many domains, as you like. This is fairly common in mid-priced hosting providers.