Forum Moderators: phranque
I used to do this is a subdirectory, but now I've got pages that require absolute reference to root, so that no longer works.
I'm thinking I could create subdomain, like: test.mydomain.com
Will that work?
How do others handle this?
If you put the whole site in a folder, the browser looks for the included file, image, or whatever starting from the root directory rather than starting from the folder. There may be a way around that using a rewrite, but I haven't figured it out yet.
Works for me with no problems. The only problem you may have is if you use canonical URLs, that is, links like <a href="http://www.example.com/images/image01.jpg"> -- Obviously, those would have to be changed to point to your test subdomain.
The beauty of URL rewriting, whether accomplished by scripts, mod_rewrite, or ISAPI rewrite, is that URLs and filepaths need not have *any* fixed relationships.
Jim
P.S. Don't forget to put an appropriate robots.txt file in that "subdomain root" subdirectory!
jdMorgan said: the same rewrites you use to 'map' the subdomain to the subdirectory will be invoked for any URL...
You use rewrites and a folder that is actually in the root directory? Interesting idea.
What's the purpose? Is that so you can do FTP without having to go below the orginal root level? I've had trouble with that, ended up setting up two different FTP connections for each site.
Would something similar work with files you want to keep "below root" for security purposes? That strikes me as risky, but maybe it's not.
Also, thanks for the heads up on canonicals. I probably did have a couple of those for no good reason!
I don't know a good short way to explain it, except by example. I use the following code in my Web root .htaccess to support a test subdomain by mapping it to a subdirectory in my single "account."
# Redirect test subdomain to subdirectory /test/
RewriteCond %{HTTP_HOST} ^test\.example\.com
RewriteCond %{REQUEST_URI} !^/images/
RewriteRule (.*) /test/$1 [L]
If you want to be 'symmetrical' about it, you could map your domain and your www subdomain to a subdirectory named 'production' and then go 'live' with the new stuff by simply renaming the 'test' subdirectory to 'production' whenever you're ready. Basically, rename or copy 'production' to 'backup', then rename 'test' to 'production.' If you use a shell script to do it, the switch-over is practically instantaneous.
The only really tricky part is if you want to do authentication. In that case, it's much easier if you do the subdomain->subdirectory rewriting in httpd.conf. This is because of the order in which Apache does things; It will do rewrites in httpd.conf, then do authentication, and then process rewrites in .htaccess. So, your test.example.com site password logins can get complicated (OK, messed-up) if you do the rewrite in .htaccess.
There are two work-arounds to this. The first is to not support authenticated subdirectories in your 'test' subdomain; Instead, you just use a RewriteCond like the one I used for /images/ above to 'share' all authenticated subdirectories between all the subdomains. The problem with this is that you then don't have a copy of the authenticated subdirectories for test purposes, so you have test those subdirectories on the 'live' site.
The other work-around is to put all your authentication code in .htaccess in the true Web-root directory or in httpd.conf. In that case, you'll need a separate section in that code for the subdomain-support subdirectory or subdirectories.
As in most things, mod_rewrite gives a lot of flexibility, but at the cost of complexity. Once you get used to the way this technique works, though, it's not so bad.
I hope that explains it somewhat clearly. :)
Jim
I'm a little embarrassed to admit that when I finally tried this out, I didn't need to do anything special. I thought I was going to have to go through a process of writing redirects and so forth as you described.
It appears that because my Web host uses cpanel, I can just create a new subdomain (like subdomain.mydomain.com) in cpanel. Then if I create a folder called "subdomain" in the main site's root folder, it automatically redirects to that folder. It's easier than making Jello.