Forum Moderators: phranque

Message Too Old, No Replies

Redirect 3rd level domain to directory outside of root?

         

mdaniels

8:51 pm on Oct 10, 2005 (gmt 0)

10+ Year Member



I am a complete Apache novice, with almost no real knowledge of how it all works. I hope I can properly explain what I would like to do.

I'm working on a redesign of a website and have created a new directory outside the public root directory where I can work on the new site. I would like to be able to access the new design via a 3rd level domain URL.

The public root directory is located at:
/host/users/myaccount/htdocs/

The new directory is:
/host/users/myaccount/newsite/

and I would like it accessed by a third level domain:
newsite.myaccount.com

I believe this can be done via the .htaccess file. I've been reading up on Apache Directives, and honestly, they don't make much sense to me. The third level domain is already created, I just need to have it point to the right place.

If someone could hlep me figure this out, I would greatly appreciate it. Or, if I'm approaching this in the wrong manner, please let me know that as well.

Thanks,

fish_eye

11:34 pm on Oct 10, 2005 (gmt 0)

10+ Year Member



You're correct in that it's very simple so long as you can add a .htaccess file to your site.

Assuming you can, can you put it into /myaccount? Is there one already there? Can you get the hosting company to point your domain to this (not /htdocs) (but don't do it yet)? Let me know and I'll post the code.

I have found that there is one condition where it (sub domains) does not work perfectly (trivial but still....) and no-one (not even Jim Morgan!) has given me a fix - not one that works on my site anyway (and I suspect this can vary depending on how my hosting company has things set up). I don't have the details to hand but can dig them up (they're in old posts in this forum and sticky mails).

The reason I'm mentioning this is because you may not need to go as far as setting up a sub-domain if you don't want - especially if it's just for testing.

You can always just set up an "index2.php" and have the links in there point to the testing sub-directory (http://www.example.com/newsite/) or simply put the index in /newsite/ and access this directly.

My experience has been that sub-domains looked really cool when I first discovered them but later proved to be more trouble than they're worth. I now just use sub-directories (which no-one but me or my clients know about - and I don't link to them so the bots dfon't find them) and then transition to the new site when tested.

If it's a high profile, high traffic site there is a way to use .htacces to easily have versions of your site (eg. /host/users/myaccount/20051010/ in production while we test with /host/users/myaccount/20051011/). Today you have http://www.example.com/ resolving to /20051010/ and tomorrow you make a one line change (in your .htaccess) to have it resolve to /20051011/. I have no had the need to do this (I just make the change - usually uploading the new index.php last - at the lowest traffic time of day (and keep my fingers crossed)).

Depends on how picky you / your customer is.

mdaniels

1:29 am on Oct 11, 2005 (gmt 0)

10+ Year Member



The /host/users/myaccount/htdocs/ directory is default public root directory and it has the .htaccess file in it. I have full access to the server, so I can add files and set permissions if needed.

I've thought about using sub-directories, and actually started one, but then decided I would rather start with a totally new clean directory, and only copy what was needed. I will be installing an open-source CMS program, which will have a completely new directory structure anyway, and not much of the old directory structure will still be around.

Your last paragraph pretty much describes exactly what I want to do. Create a totally new site for testing that I can access from the sub-domain, but when it comes to make it live, I can easily change it over.

I just don't know much about editing the .htaccess to make this happen!

mdaniels

1:31 am on Oct 11, 2005 (gmt 0)

10+ Year Member



Forgot to mention, I could probably get the hosting company to point to the /myaccount/ rather than /htdocs/. But I would have to check into it first.

fish_eye

1:56 am on Oct 11, 2005 (gmt 0)

10+ Year Member



Firstly - you may as well leave things as they are currently set up and put the test directory below htdocs:

/host/users/myaccount/htdocs/newsite

2) Back up your .htaccess!

3) Include the following in the .htaccess:

RewriteCond %{HTTP_HOST} ^testname\.example\.com
RewriteCond %{REQUEST_URI}!^/newsite/?
RewriteRule ^(.*)$ /newsite/$1 [L]

What this will do is redirect requests to the "testname" sub-domain to the testing subdirectory (of the root) called /newsite/ (but only if it's not already requested).

The glitch with this that I found is that if someone types in http://example.com/newsite they get in okay (and I'd rather have them rewritten to [testname.example.com)....] As I said, minor problem - and on reviewing sticky mails I think Jim did actually find a solution!

The next bit I'm not so sure about and I'd rather not post yet as mod_rewrite is a very fiddly thing - I'll have a look through some old stuff I had when I was going to do this. Basically you simply remove the above (or change it to the sub-dir of the next version) and direct all "normal" requests to:

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

(...which is why I put dates in my earlier example....)

The idea is that your current and future sites then always exists as sub-dirs (dated versions) of the root.... the current is www (or nothing) and others are "testname".

The other advantage of this is you can revert to a previous version very simply if you find a glitch after a couple of hours (never!). Space may be an issue if you have lots of big files and for this to be simple EVERYTHING has to be duplicated on all versions.

More later (or someone else will assist with the end of the story).