Pointing non-www to a sub-directory

I might be going about this all wrong so let me know if I am.

I am creating software that allows people to sign up and have their own sub-domain on my website.

So say my website is ben.com, they could have their own sub-domain called juice.ben.com.

When they type their sub-domain juice.ben.com in their address bar, it will load the contents in a root directory.

I have also set-up a .htaccess redirect to redirect www.ben.com to ben.com. Not sure if this matters with my question but I thought I’d mention it.

Ok, so basically what I think I need to do is put the software they they’ve signed up to in the root directory. So when someone goes to juice.ben.com, they will be pointed to the root directory (I beleive I can set-up wild card sub-domains with my host) and the software will then analyse their sub-domain and then display their account.

Now, if someone just types in ben.com into their browser, I want it to show the contents of the ben.com/_website/ folder but still show in the address bar that they are still in the root directory.

Hopefully I am making sense :slight_smile:

Is this possible with htaccess? If so, what do I need to do?

Mr. T,

Yes, you’re making sense but, no, you didn’t bother reading the thread at the TOP of the (non-sticky) threads! Same answer (unless you can setup Dynamically configured mass virtual hosting).

Regards,

DK

Hey David,

Sorry I missed what you are referring to. Is this the thread? http://www.sitepoint.com/forums/showthread.php?t=673449

If yes, I seems to be different to what I am needing. Otherwise if you’d be kind enough to point out which thread it is? I’ve had a look through some of the posts but can’t seem to find my answer :frowning:

Appreciate your help :slight_smile:

Yes, I’m pretty sure that’s the one.
It’s also about forwarding all subdomains to the main domain, isn’t that what you want?

Almost… However, if possible, I am wanting it to not redirect but still say it’s viewing the root directory even though it’s pulling the contents from the /_website/ folder…

Here is the code I’m paying around with… is it even possible?

RewriteCond %{HTTP_HOST} ^domain\.com$
RewriteCond %{HTTP_HOST} !^domain\.com\/_website$
RewriteRule (.*) http://domain.com/_website/index.php [L]

It’s similar to what I want but I don’t want it to redirect, I want it to pull up the contents of the /_website/ folder but still appear to be viewing the root directory… Is this possible?

I’ve almost got it working… if I type in http://test.com/randomfile.php it loads the /_website/index.php file…

But if I type in http://test.com/ or http://test.com/inex.php it loads the root directory…

Is these some condition I can add in that makes both of those still point to the /_website/index.php file?

Here is my current code:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^test\\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /_website/index.php [L]

Mr. T,

The point of my post is that subdomains are NOT automatically created (generally). If you don’t have the Dynamically configured mass virtual hosting setup AND you have NOT created a subdomain via your control panel (which modifies the DNS records), you will NOT receive a request for subdomains which you imply that you are expecting. This is a DNS question, NOT a mod_rewrite question.

Once you have either “mass virtual hosting” configured OR the subdomains configured, the DNS should be pointed at the subdirectory for you so your DocumentRoot’s .htaccess will NEVER be processed, i.e., the question is for a problem which doesn’t exist.

Regards,

DK

Mr. T,

WIth the problems in the mod_rewrite (assuming mass virtual hosting IS setup), the answer is no! The _website directory is not a part of the domain name, thus, not a part of {HTTP_HOST}. What you need to match (again, assuming that all mass virtual hosts share the DocumentRoot of the Main domain) is the subdomain and use that with an INTERNAL redirect to the subdirectory associated with that subdomain’s account.

Okay, my questions for you are:

  1. What happened to “juice”? The subdomain was how this all started.

  2. Can you confirm that you have “Dynamically configured mass virtual hosting” setup?

  3. When you can respond to those, we’ll get to the regex and Apache variables which are required for your original question - or the new question as your “specificity” seems to have changed. That will be something like

RewriteEngine on
# capture subdomain (to use for the redirection to the account's subdirectory)
RewriteCond %{HTTP_HOST} ^(www/.)?([a-z]+)\\.example\\.com$ [NC]
# Be sure that www was NOT what you captured!
RewriteCond %2 !^www$ [NC]
# Don't loop on the subdirectory!
RewriteCond %{REQUEST_URI} !^%1/
# Redirect everything to the subdirectory
RewriteRule ^(.*)$ %1/$1 [L]
# this assumes Apache 2.x

Regards,

DK

Hi DK,

This is the first time I’ve done this so I apologize if I am messing you around and confusing matters. I appreciate your patience :slight_smile:

I’ll update you on where I’m at :slight_smile:

OK, so I’ve had my web host set-up wild card sub domains just today (they said the ‘Dynamically configured mass virtual hosting’ link you gave was a little too complex for my CPanel set-up) so that juice.ben.com or any other sub domain is now pointing to ben.com. This is all working and I’m happy with this side of things :slight_smile: So I guess we can forget about the sub domains now.

Now what I am trying to achieve is if they aren’t visiting the website through a sub domain, the contents of the ben.com/_website/ folder will appear but without redirecting.

Some examples…

If they go to:

http://ben.com/about/

It will really load:

http://ben.com/_website/about/

If they go to:

http://ben.com/test.php

It will really load:

http://ben.com/_website/test.php

So, that’s where my latest code come in. Here it is again

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^ben\\.com$ [NC]

# these check to see if the file exists which isn't really 
# what I want but I get a 500 error if I remove these.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . /_website/index.php [L]

Now, this code works for the above just fine. The only time it breaks or doesn’t work is when I go to:

http://ben.com/
http://ben.com/index.php

It loads:

http://ben.com/
http://ben.com/index.php

Rather than:

http://ben.com/_website/
http://ben.com/_website/index.php

So all I have to do is somehow tell it that if they go to either of the above, it needs to show the /_website/ folder instead.

I had a play with your code. I added in the /_website/ folder and changed a few things to suit but it’s giving me a 500 error…

RewriteEngine On

# Redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\\.ben\\.com$ [NC]
RewriteRule ^(.*)$ http://ben.com/$1 [R=301,L]

# Capture main domain (to use for the redirection to the /_website/ subdirectory)
RewriteCond %{HTTP_HOST} ^ben\\.com$ [NC]

# Don't loop on the subdirectory!
RewriteCond %{REQUEST_URI} !^_website/

# Redirect everything to the subdirectory
RewriteRule ^(.*)$ _website/$1 [L]

Am I making sense now?

Once again I appreciate your help and patience with this. Like I said this is all new to me so I’m having trouble explaining everything correctly.

Apache describes REQUEST_URI as


The resource requested in the HTTP request line.

So your code won’t work because redirects to _website are internal (no 301) redirects, therefore they will not be in the HTTP request line, and therefore %{REQUEST_URI} will never match ^_website (imho).

As for the .htaccess


RewriteEngine On

# Strip off www
RewriteCond %{HTTP_HOST} ^www\\.ben\\.com$ [NC]
RewriteRule (.*) http://www.ben.com/$1 [L,R=301]

# If the request is not for ben.com (but a sub subdomain) ...
RewriteCond %{HTTP_HOST} ^[\\-\\w+]+\\.ben\\.com$
# ... skip the following RewriteRule
RewriteRule . - [S=1]

RewriteRule . /_website/index.php [L]

(not tested)

I’m wondering why you would redirect everything to ben.com to _website, and not the other way arround, i.e., interally redirect all request to subdomains to _subdomain, and let ben.com just be as it is.
Do you have a reason for this?

Mr. T,

Aw, changing specifications is the best way to DOUBLE (or TRIPLE) the price of the responses! Just ask any good contracting officer (or independent contractor).

Okay, your host has done you a MAJOR favor by allowing dynamic virtual mass hosting (unlimited subdomains) AND pointing them at your DocumentRoot. That will make the code I had given you work PERFECTLY!

ARGH! THEN you go and change the directory level for … well, what for? All you have is that it must be changed for ALL files and directories which do not exist! Therefore, what did you expect with http://ben.com/ (doesn’t the DirectoryIndex exist?) and ttp://ben.com/index.php (isn’t that your DirectoryIndex again?)? If you go “punching holes” in your regex (with RewriteCond exceptions) then you won’t be able to access your domain’s Home Page! Ergo, SPECIFICITY (say what you’re trying to do and see where the problems exist - this one should be OBVIOUS)!

THEN go lookup what Apache.org has to say about RewriteBase. Since it’s designed to UNDO the effects of a mod_alias (and you’re NOT showing that you’re using any mod_alias directives), GET RID OF THAT!

Using only the dot character in your regex will NOT match the empty {REQUEST_URI} string so I will use .? instead. Again, SPECIFICITY!

I had a play with your code. I added in the /_website/ folder and changed a few things to suit but it’s giving me a 500 error…

...
# Capture main domain (to use for the redirection to the /_website/ subdirectory)
[COLOR="Red"]# WHAT do you want to capture the main domain for? That doesn't make sense![/COLOR]
...

No, you’re NOT making sense (because of the change of direction AND the simple fact that your original intention is now possible).

Remon,

The {REQUEST_URI} CHANGES as one progresses through mod_rewrite statements with the redirections ({THE_REQUEST} does not change).

Regards,

DK

Off Topic:

didn’t know that, I’ll keep it in mind :slight_smile:

Remon,

Aw, another “secret” uncovered! Pretty soon, we’ll be out of a job in here!

Regards,

DK