Forum Moderators: phranque

Message Too Old, No Replies

Real and virtual subdomains problem

Real and virtual subdomains problem

         

Psychopsia

4:08 am on Aug 15, 2006 (gmt 0)

10+ Year Member



Hi!

I'm having problems using real and virtual subdomains.

I have created virtual subdomains with wildcards like:

RewriteCond %{HTTP_HOST}!^www\.
RewriteCond %{HTTP_HOST} ^([^.]+)\.example.net
RewriteCond %{REQUEST_URI}!^/%1/
RewriteRule (.*) file_on_server/%1/ [nc]

This is working properly.

But, today created a real subdomain in Cpanel (http://s.example.net) and when type this, redirects to the main website [s.example.net...] ('intro' is another rewrite rule in the same htaccess).

The Zone File looks like:

* - 14400 - CNAME - example.net.
s - 14400 - A - MyIP
www.s - 14400 - A - MyIP

Everything is working fine, except the "s" real subdmain.

I tried to disable the virtual subdomains in htaccess to see what happen, but it doesn't work as expected :?

Any ideas on what is happening here?

Thanks in advance :)

jdMorgan

12:51 pm on Aug 15, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This line will not work, unless you're using a just-released version of Apache with a new undocumented feature: Unless this is the case, the value on the right-hand side of a RewriteCond must be a fixed string, not a variable.

RewriteCond %{REQUEST_URI} !^/%1/ [nc]

Also, even if it did work, it would leave you in a position that no subdomain could be allowed to match the name of a 'real' subdirectory without causing a major problem for your site. For example, if somone requested a subdomain of "images.example.net", that would allow direct access to the "iamges" subdirectory on your site.

A better approach might be to do this:


RewriteCond %{REQUEST_URI} [b]!^/sub_[/b]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.net
RewriteRule (.*) /[b]sub_[/b]%1/$1 [L]

Here, each subdomain-subdirectory is 'tagged' with a path-prefix of "sub_" that identifies it as a virtual subdomain subdirectory, making prevention of that problem easy, and relying on a fixed string to be tested in the loop-prevention RewriteCond. With this construct, a request for "foo.example.net/bar.html will be internally rewritten to the path "/sub_foo/bar.html".

The "sub_" prefix shown here is arbitrary; You can use any sufficiently-unique string that you like.

Jim

Psychopsia

4:53 pm on Aug 15, 2006 (gmt 0)

10+ Year Member



Thanks Jim.

I changed the rule but it still doesn't work, also I deleted everything inside the htaccess to see if this is the problem, but continues redirecting to the main website, not to the directory view.

Can be the zone file or Apache config the problem?

The new rule looks (some changes based on your rewrite rules in other posts):

RewriteCond %{HTTP_HOST}!^(www¦s)\.example\.net [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([0-9a-z]+)\.example\.net [NC]
RewriteRule ([0-9]+)? dir/file.php?id=%2&page=$1 [L]

Thanks for your help!

jdMorgan

5:22 pm on Aug 15, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> but continues redirecting to the main website,

That's a good clue right there, because the code we are discussing doe not redirect, it does an internal rewrite. You should see no change in the browser address bar. If you do, then this code is not at fault -- Code in a higher-level .htaccess file, or in httpd.conf is interfering or is unconfigured or misconfigured.

Also, please don't make unrelated code changes until we get this working -- changing things around only confuses things. I don't know what (www¦s) is intended to do, but let's not complicate things. Also, do not use [NC] unless you want to have major problems with independently-existing upper- and lower-case subdomains and subdirectories -- I suggest you accept only lowercase, and therefore, the [NC] flags should be omitted.

Jim

Psychopsia

5:52 pm on Aug 15, 2006 (gmt 0)

10+ Year Member



Sorry for the confussion, will explain why I said 'redirecting':

The currently htaccess looks like:

Options +FollowSymlinks
RewriteEngine on

RewriteCond %{HTTP_HOST}!^www\.
RewriteCond %{HTTP_HOST} ^(www\.)?([0-9a-z]+)\.example\.net
RewriteRule ([0-9]+)? dir/file.php?id=%2&page=$1 [nc]

RewriteRule ^cover/?$ dir/index.php [nc]

So, in "/index.php" tells the browser to go to "http://www.example.net/cover/".

When I created the real subdomain "http://s.example.net" *redirects* to "http://s.example.net/cover/"

Do you think the hosting admin misconfigured the httpd.conf?

jdMorgan

11:37 pm on Aug 15, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's likely, because none of your code does a redirect -- all of your rules do internal rewrites, and no change should be visible in the browser address bar.

Jim

Psychopsia

12:31 am on Aug 19, 2006 (gmt 0)

10+ Year Member



Yes, every rule is internal.

But after delete those rules, the problem persists, I mean the real subd doesn't work :?