homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

subdomains versus cakephp
redirecting if requested subdomain exists

5+ Year Member

Msg#: 4046649 posted 11:09 pm on Dec 20, 2009 (gmt 0)

Hi everyone, I am using CakePHP as the framework for a website I am developing, and as part of that, it has an htaccess file which basically changes the webroot to be in a different place. Here is the htaccess which currently works:

RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]

I am pretty new to htaccess, and what I am trying to do, I have no idea how!

Basically, if someone goes to [website.com.au...] or [website.com.au,...] they should be managed as per the htaccess rules above - this is currently working fine.

However, there needs to be a folder that contains micro-sites, which are not setup as subdomains, but if they are requested as a subdomain, it should redirect the user through to them.. hoping I'm making sense here!

So, if I go to [mysite.website.com.au...] OR [website.com.au...] OR [website.com.au...] I would like it to display content from [website.com.au...]

The way I see it, is that a few things need to happen. If something (eg mysite) exists before website.com.au and it is not www, check to see if the directory (/member_sites/mysite) exists.

If there is nothing before website.com.au or there is but it is www, check to see if the first thing after .com.au is a directory. i.e. website.com.au/mysite

If the above return true, display the content from /member_sites/mysite

If the above return false, simply run the CakePHP rules.

Is that even possible? If someone was able to help me out with this it would be really appreciated :)

Hope you can help,




WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

Msg#: 4046649 posted 12:01 am on Dec 21, 2009 (gmt 0)

When you say "display content from" you need to clarify if that content is to be returned with a "200 OK" status for the originally requested URL, or whether you want to issue a 301 redirect so that the browser makes a new request for the correct URL before the content is delivered to the browser.

If you let multiple URLs directly display the same content, you will have a created a Duplicate Content problem.

Forget about files and folders and the internal workings of the server for a minute, and just think about URLs. What URL represents the canonical URL for the content? What other URLs could that content also be returned for? Knowing this, allows us to define your canonicalisation redirects. Once you have one URL for the content, a rewrite can be implemented to accept that URL request and fetch the content from wherever it actually resides inside the server.


5+ Year Member

Msg#: 4046649 posted 12:31 am on Dec 21, 2009 (gmt 0)

Hi there, well it doesn't matter whether it redirects through to the member site, or if it just displays content, so long as when someone types in any of the following:


They see the content they are looking for. (held at website.com.au/member_sites/mysite

I guess what I was thinking is that ideally, whatever they typed into the URL would be used as the base, then files in the website appended to that, but if that is likely to cause issues, I'm sure its of no great concern if the URL displays /member_sites/mysite..

Does that help, or am I missing the point?


WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member

Msg#: 4046649 posted 1:40 am on Dec 21, 2009 (gmt 0)

The first step is to take a look at the RewriteCond directive and the %{HTTP_HOST} variable which contains the hostname (loosely, the 'domain') requested by the user/client/browser/robot. Also look at the mod_rewrite documentation to find out how to negate a pattern-match -- you'll need that as well.

That will be a good start to qualifying the action of your rule based on the requested hostname.

You should think about how you want to present your users' 'minisites' to the world. You can either go with the example.com.au/users/username approach or --if you have a dedicated IP address for your server-- you can give each of them a subdomain such as username.example.com.au. In this case (subdomain method), you'll use an internal rewrite to map the requested username.example.com.au URLs to the /users/username filepaths anyway, but it will sure look a lot 'cleaner' to the users...

As far as www.example.com.au and example.com.au go, pick one or the other now. Redirect the one you don't prefer to the one that you do prefer in order to avoid duplicate content and to simply all of the other rules you need (both today and in the future).

I've given only the first step above because it's important to decide *exactly* what you want to do before asking for assistance on coding approaches. You also need to think about *all* that you want to do, so as to avoid wasting effort on implementing a method that's good, but won't support all of the 'features' you want.

What I'm saying is, make a solid and complete plan of how you want your site to work, what you want the URLs to look like, and how you want to arrange your directories and files before you try to write the first line of code -- That will save a lot of effort and may prevent a lot of grief.



5+ Year Member

Msg#: 4046649 posted 2:06 am on Dec 21, 2009 (gmt 0)

Thanks Jim,

This is what I have based on your comments and what I have found in my searching..

#if www or no www have been used, send to vps.website.com.au (when live,
#only check for absence of www and send to www
RewriteCond %{HTTP_HOST} ^www\.website\.com\.au$ [OR]
RewriteCond %{HTTP_HOST} ^website\.com\.au$
RewriteRule (.*) [vps.website.com.au...] [R=301,L]

#if host doesn't start with vps, send to /member_sites/<subdomain>
RewriteCond %{HTTP_HOST} !^vps\.website.com.au$
RewriteRule (.*)\.website\.com\.au/ [vps.website.com.au...]

#check if requested file exists, if it does, serve it, if not, send to cakephp webroot
RewriteCond %{DOCUMENT_ROOT}/member_sites/%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/member_sites/%{REQUEST_URI} -d
RewriteRule (.*) [vps.website.com.au...] [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]

I might be way off here, but hopefully I am getting close. If I type in [vps.website.com.au...] I successfully get redirected to /member_sites/mysite. If I specify a page in the cakephp site, I get to that page, so I think I'm close.

The main problems with the above are that if I just put in [vps.website.com.au...] it takes me to [vps.website.com.au...] because the REQUEST_URI is empty, and member_sites/ is a directory.

The other problem is that if i use [mysite.website.com.au...] it results in 404 not found..

Am I close with this?




5+ Year Member

Msg#: 4046649 posted 2:09 am on Dec 21, 2009 (gmt 0)

with regards to your question of how it will be displayed, ideally I would like it to be displayed as [mysite.website.com.au...] but as this is a redevelopment project, there are already sites located at [website.com.au...]

So in an absolutely ideal world, it would redirect from .com.au/mysite to mysite.website.com.au and then display it from there...


WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member

Msg#: 4046649 posted 4:47 pm on Dec 21, 2009 (gmt 0)

Well here's two clean-ups/fixes for the first two rule to start.

When reporting trouble, please specify both the 'wrong' behavior you're seeing *and* the expected/desired behavior; As we answer many questions throughout the day and are not intimately familiar with your site, it's otherwise a bit difficult to 'keep up.'

# if www or no www is requested, rewrite to vps.example.com.au
# (when live, only check for absence of www and redirect to www)
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com\.au
RewriteRule ^(.*)$ http://vps.example.com.au/$1 [R=301,L]
# if host doesn't start with vps and requested URL-path is
# non-blank, rewrite to /member_sites/<subdomain>/<url-path>
RewriteCond %{HTTP_HOST} !^vps\.example\.com\.au
RewriteCond %{HTTP_HOST} !^([^.]+)\.example\.com\.au
RewriteRule ^(.+)$ http://vps.example.com.au/member_sites/%1/$1 [L]

Note that no end-anchoring is used on the "domain" patterns. If you wish to end-anchor the hostname patterns, then you'll need to modify hostname patterns to handle FQDN hostnames and trailing port numbers. For example, a request for www.example.com.:80 is possible and perfectly-valid.

I'm not sure what your intent is for the last two rules in your post. But be aware that RewriteConds apply only to the single RewriteRule that follows them; If the intent is that the last rule should also be qualified by the same conditions as the second-to-last rule, then you'll need to replicate those RewriteConds.


Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved