Forum Moderators: phranque

Message Too Old, No Replies

Wildcard subdomains

Is this possible

         

fcxsebs

8:35 pm on Feb 28, 2007 (gmt 0)

10+ Year Member



Hi there,

Here's what I'm trying to do.

I have a website www.mywebsite.com, I'd like to be able to go to

sebs.mywebsite.com

Which would actually RewriteRule and do something like www.mywebsite.com/index.php?blah=sebs in the background.

Is that possible without creating the DNS entries for the sub domains?

Thanks for you help

jdMorgan

10:50 pm on Feb 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes and no... It depends on how your dns and server are currently set up.

IF your DNS points all subdomains to your server's IP address, and IF your server is set up to point all subdomains to your "main" domain's file space, then this is simply a matter of using mod_rewrite to get the HTTP_HOST header from the incoming request, and rewrite it to your handler script as a parameter.

Try requesting an odd subdomain from your server; If you see your main home page, then all you need to do is add the mod_rewrite code.

If you get "Host cannot be found" then you need to fix your DNS setup to define a wildcard subdomain record, e.g. "*.example.com. A 192.168.0.1".

If you get "<something> does not exist on this server," then you need to investigate/correct/change the server configuration.

Jim

fcxsebs

12:49 pm on Mar 1, 2007 (gmt 0)

10+ Year Member



Thanks jdMorgan,

I will ask my hosting company, but I doubt I will be able to do that :(

So let say that I do have blah.mywebsite.com, how would I get the blah get passed as a query string?

Thanks again for your help

jdMorgan

1:54 pm on Mar 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This search [google.com] turns up several on-topic threads.

Jim

fcxsebs

8:06 pm on Mar 1, 2007 (gmt 0)

10+ Year Member



Thanks as always JD,

Here's what I have now:

Options +FollowSymlinks
RewriteEngine on
RewriteBase /

# If no-www domain requested, externally redirect to www domain
RewriteCond %{HTTP_HOST} ^mydomain\.com
RewriteRule (.*) [mydomain.com...] [R=301,L]

# If www+subdomain domain requested, externally redirect to subdomain without "www"
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.mydomain\.com
RewriteRule (.*) [%1.mydomain.com...] [R=301,L]

# If subdomain+www domain requested, externally redirect to subdomain without "www"
RewriteCond %{HTTP_HOST} ^([^.]+)\.www\.mydomain\.com
RewriteRule (.*) [%1.mydomain.com...] [R=301,L]

# If subdomain requested, rewrite home page requests to index.php with query string user=subdomain & page="home"
RewriteCond %{REQUEST_URI}!^/index\.php
RewriteCond %{HTTP_HOST}!^www\.mydomain\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com
RewriteRule ^$ /index.php?qs1=%1 [QSA,L]

# Redirect the first two folder to the querystring
RewriteCond %{REQUEST_URI}!^/index\.php
RewriteCond %{REQUEST_URI}!^/images
RewriteCond %{REQUEST_URI}!^/styles
RewriteRule (.+)/(.+) index.php?qs2=$1&qs3=$2 [QSA,L]

# Redirect the first folder to the querystring if it doesn't have two folder
RewriteCond %{REQUEST_URI}!^/index\.php
RewriteCond %{REQUEST_URI}!^/images
RewriteCond %{REQUEST_URI}!^/styles
RewriteCond %{REQUEST_URI}!^/(.+)/(.+)
RewriteRule (.+) index.php?qs2=$1 [QSA,L]

The first 3 commands works fine, but it's the last three, they work seperatly but not togheter.

I want that if I go to: abc.mydomain.com/blah/foo it rewrites to www.mydomain.com/index.php?qs1=abc&qs2=blah&qs3=foo

So I think the problem is in the QSA, which I thought that it happenned to the querystring, but maybe not they way I thought.

Also, the L switch, it's to say it's the last command, so to make sure I understand right, if it does the first command, it stops, redirect and then redo the .htaccess but obviously skip the first command because it is not true anymore, right?

Another thing, sorry about that, I just want to understand it and not just copying it, this part:

RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com

isn't the same as doing

RewriteCond %{HTTP_HOST} .+\.mydomain\.com

I'm referring to this for the expressions:

. (full stop) - match any character
* (asterix) - match zero or more of the previous symbol
+ (plus) - match one or more of the previous symbol
? (question) - match zero or one of the previous symbol
\? (backslash-something) - match special characters
^ (caret) - match the start of a string
$ (dollar) - match the end of a string
[set] - match any one of the symbols inside the square braces.
(pattern) - grouping, remember what the pattern matched as a special variable

Thanks

fcxsebs

1:42 pm on Mar 2, 2007 (gmt 0)

10+ Year Member



Ok, well, I finally got it to work, I figured out why they weren't working togheter, it's because it was redirecting to an .php page and one of my RewriteCond was that it shouldn't be an .php page.

But now the problem(Well not really a problem since it works) is that I am sure that he could be compacted/optimize!

Here's what I have(I didn't include my first three command since they work and does not affect this):

# If subdomain requested, rewrite home page requests to index.php with query string user=subdomain & page="home"
RewriteCond %{REQUEST_URI}!^/(.+)\.php
RewriteCond %{HTTP_HOST}!^www\.mydomain\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com
RewriteRule ^$ /index.php?qs1=%1 [QSA,L]

# Redirect the first two folder to the querystring
RewriteCond %{REQUEST_URI}!^/(.+)\.php
RewriteCond %{REQUEST_URI}!^/images
RewriteCond %{REQUEST_URI}!^/styles
RewriteCond %{HTTP_HOST}!^www\.mydomain\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com
RewriteRule (.+)/(.+) /index.php?qs1=%1&qs2=$1&qs3=$2 [QSA,L]

# Redirect the first two folder to the querystring
RewriteCond %{REQUEST_URI}!^/(.+)\.php
RewriteCond %{REQUEST_URI}!^/images
RewriteCond %{REQUEST_URI}!^/styles
RewriteRule (.+)/(.+) /index.php?qs2=$1&qs3=$2 [QSA,L]

# Redirect the first folder to the querystring if it doesn't have two folder
RewriteCond %{REQUEST_URI}!^/(.+)\.php
RewriteCond %{REQUEST_URI}!^/images
RewriteCond %{REQUEST_URI}!^/styles
RewriteCond %{REQUEST_URI}!^/(.+)/(.+)
RewriteCond %{HTTP_HOST}!^www\.mydomain\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com
RewriteRule (.+) /index.php?qs1=%1&qs2=$1 [QSA,L]

# Redirect the first folder to the querystring if it doesn't have two folder
RewriteCond %{REQUEST_URI}!^/(.+)\.php
RewriteCond %{REQUEST_URI}!^/images
RewriteCond %{REQUEST_URI}!^/styles
RewriteCond %{REQUEST_URI}!^/(.+)/(.+)
RewriteRule (.+) /index.php?qs2=$1 [QSA,L]

So I am laying out every possible scenario, isn't there a way to compact this?

Also, another thing, in this one:

RewriteCond %{REQUEST_URI}!^/(.+)\.php
RewriteCond %{HTTP_HOST}!^www\.mydomain\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com
RewriteRule ^$ /index.php?qs1=%1 [QSA,L]

I am rewriting to /index.php, how can I do it so it is whatever page I am on?

And, the %1 is the variable from the RewriteCond? Can it only be one?

Like in this code:

# Redirect the first two folder to the querystring
RewriteCond %{REQUEST_URI}!^/(.+)\.php
RewriteCond %{REQUEST_URI}!^/images
RewriteCond %{REQUEST_URI}!^/styles
RewriteCond %{HTTP_HOST}!^www\.mydomain\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com
RewriteRule (.+)/(.+) /index.php?qs1=%1&qs2=$1&qs3=$2 [QSA,L]

Shouldn't the subdomain be %2 and the subdomain is overwrite the first one?

Thanks for your help, I know I'm asking a lot, but this is very interesting/useful and I was to understand it as much as possible.

fcxsebs

2:18 pm on Mar 5, 2007 (gmt 0)

10+ Year Member



Someone has any idea?

jdMorgan

2:34 pm on Mar 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



We can't write and test your entire file here -- This is a discussion forum, not a free coding service. However, here's an example of one of your sections in more-compact form:

# Redirect the first two folder to the querystring
RewriteCond %{REQUEST_URI} !^/(.+\.php$¦images/¦styles/)
RewriteCond %{HTTP_HOST} !^www\.mydomain\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com
RewriteRule ^([^/]+)/(.+)$ /index.php?qs1=%1&qs2=$1&qs3=$2 [QSA,L]

As you surmise, the %1 back-references the first pattern in the last-matched RewriteCond; As with the $1-$9 back-references from RewriteRule patterns, these RewriteCond pattern back-references can range from %1-%9, but only from the last-matched RewriteCond.

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

IMPORTANT: Replace the broken pipe "¦" characters above with solid pipe characters before use; Posting on this forum modifies the pipe characters.

Jim

fcxsebs

2:42 pm on Mar 5, 2007 (gmt 0)

10+ Year Member



"We can't write and test your entire file here -- This is a discussion forum, not a free coding service."

I know that and I totally agree with you on this, but I add a lot of questions and needed you guys expertise, right, that's what it is for, advice/opinion and helping each other out.

You didn't have to give me the code, although I appreciate it, just explaining how would of been enough, that way I would understand.

Thanks for answering some of my questions, I'm trying to understand, don't get me wrong, it's just that what I find on the net, it is pooly documented.

Thanks Jim.