 |
 |
.htaccess and root directory
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: Canada
Status:
Offline
|
|
Is it possible to use a .htaccess file to define a new root directory for the forward slash? Say, for instance, that I have the following:
A site: http://www.mydomain.com/
A user site: http://www.mydomain.com/~user/
Is it possible to use a .htaccess file in /Users/user/Sites/ so that if the user's webpage requires "/image/pic.jpg", Apache will serve "/Users/user/Sites/image/pic.jpg" instead of "/Library/WebServer/Documents/image/pic.jpg"?
If not, is there any other way of setting up a similar function other than using a virtual host?
Edit: I found some information on mod_rewrite. If Apache is running mod_rewrite, is it possible to use the "Moved DocumentRoot" example for my situation? Would I use "/Users/user/Sites/" instead of "/e/www/"?
(Last edited by dtriska; Jan 15, 2003 at 12:19 AM.
)
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Jul 2001
Location: NC
Status:
Offline
|
|
Why not just change
DocumentRoot "/Library/WebServer/Documents"
in the /etc/httpd/httpd.conf config file to point to your Sites directory? As far as I know, a .htaccess file in your Sites directory wouldn't even be consulted unless the request contained ~<username> in the URL. Of course you could address this with symbolic links. Mod_Rewrite should definitely work if you want to do this for only specific files.
|
|
Gary
A computer scientist is someone who, when told to "Go to Hell", sees the
"go to", rather than the destination, as harmful.
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: Canada
Status:
Offline
|
|
Originally posted by Gary Kerbaugh:
Why not just change
DocumentRoot "/Library/WebServer/Documents"
in the /etc/httpd/httpd.conf config file to point to your Sites directory?
Because I still need to use http://www.mydomain.com/
Originally posted by Gary Kerbaugh:
As far as I know, a .htaccess file in your Sites directory wouldn't even be consulted unless the request contained ~<username> in the URL. Of course you could address this with symbolic links. Mod_Rewrite should definitely work if you want to do this for only specific files.
Can you elaborate on this?
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Jul 2001
Location: NC
Status:
Offline
|
|
Originally posted by dtriska:
Can you elaborate on this?
I definitely should, as I can see how what I said was unclear. What I meant by "for only specific files" was the case that no other web page references /image/pic.jpg. In that case you could simply make a symbolic link from /Library/WebServer/Documents/image/pic.jpg to /Users/user/Sites/image/pic.jpg. Similarly, you could use Mod_alias to do that. Mod_Rewrite would probably be overkill.
However, if you want web pages in the document root that reference /image/pic.jpg to get /Library/WebServer/Documents/image/pic.jpg and web pages in /Users/user/Sites/ that reference /image/pic.jpg to get /Users/user/Sites/image/pic.jpg, then the situation is very different. (you should have spelled this out if this is what you wanted) The problem is that normally the web page is sent to the browser, the browser reads /image/pic.jpg and then it sends a request for http://www.mydomain.com/image/pic.jpg.
I thought that would be an insurmountable problem. (without virtual hosts) However, I reread the docs on the [R] flag and I've changed my mind. I've never used it but I'm beginning to think that your proposal will work with that flag. If you put the rewrite rule:
RewriteEngine on
RewriteRule ^/$ /~user/ [R]
in the .htaccess file of the /Users/user/Sites/ directory, then the HTTP response of 302 (MOVED TEMPORARILY) should cause the browser to send a request for http://www.mydomain.com/~user/image/pic.jpg when it reads the URL /image/pic.jpg. (thinking the whole site had moved) Excellent thinking! I apologize, I've never used the [R] flag. OK, you convinced me, so give it a try! You will let us know how it went, eh?
Edit: There is one thing I'm not sure of. What would the server do to the original request for the file that contained the <img > link? Mod_Rewrite and Mod_Alias would be acting on that URL. I thought of also using a
RewriteBase /
rule but that sounds like an infinite loop. I'm less optimistic now but at this point you have to experiment anyway. It's too interesting to not look into.
(Last edited by Gary Kerbaugh; Jan 16, 2003 at 03:07 AM.
)
|
|
Gary
A computer scientist is someone who, when told to "Go to Hell", sees the
"go to", rather than the destination, as harmful.
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Jul 2001
Location: NC
Status:
Offline
|
|
I think that you should forget the last suggestion in my previous post. If you want web pages in the document root that reference /image/pic.jpg to get /Library/WebServer/Documents/image/pic.jpg and web pages in /Users/user/Sites/ that reference /image/pic.jpg to get /Users/user/Sites/image/pic.jpg, then my next thought is to use a RewriteRule in conjuction with a RewriteCond that checked for an HTTP_REFERER containing "~user". This would be done for the site as a whole and could be done for any number of users. I'm thinking this should be done on a file by file basis, as you wouldn't want to keep the user from linking to the main site. This is roughly what I had in mind: (untested)
RewriteEngine on
RewriteCond %{HTTP_REFERER} ^.*~user/*$
RewriteRule ^(/image/pic.jpg)$ /Users/user/Sites$1 [L]
If you wanted to hide the entire images directory from the users site, you could replace the "pic.jpg" with ".*". Hence the rules don't have to be written file-by-file but you would still have to decide to what the rewrite should apply on a file-by-file basis.
This situation isn't what I thought you had in mind when I first read your post and I'm obviously improvising. I've always been impressed with Mod_Rewrite but have never needed all that power. Thus, we are learning this together and considerable testing and refinement will be required. However, it's fascinating; I just hope I've suggested something that contains at least the seed of a viable solution.
|
|
Gary
A computer scientist is someone who, when told to "Go to Hell", sees the
"go to", rather than the destination, as harmful.
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|