Forum Moderators: phranque
/very/long/path/to/the/userhome
I'd like to be able to use shorter, more conventional server paths, like this:
/home/userhome
Is there any way I can add an alias, or a rewrite, or something, to the httpd.conf file to be able to use those shorter server paths for virtual hosts, without actually changing any of the directory structure?
Looking at the Apache docs, I'm guessing the following two are most likely to be useful for the RewriteCond part:
PATH_INFO
DOCUMENT_ROOT
Of course I realize I could be wrong on that.
Unfortunately I don't have the slightest idea where I'm going with the RewriteCond; I just know that the RewriteRule line has to look something like this:
RewriteRule ^/usr/local/etc/httpd/htdocs/$ /home/ [R=301,L]
If someone can help me out with the specifics for this task, I'd sure appreciate it!
Thanks a lot,
Matthew
If this isn't enough to help, I'd suggest using the RewriteLog and RewriteLogLevel directives, which will help you debug your RewriteRule (I'd suggest doing this on either a seperate physical machine or setting up a dedicated <VirtualHost> block for testing; the RewriteLog can get REALLY full REALLY fast. =)
If you run into problems, feel free to post specifics (attempted rules, RewriteLog data, etc) and I'll be happy to point you in the right direction.
Thanks for the tips. I thought a rewrite rule always required a condition; it's useful to know that it doesn't.
Using what you suggested, and trying to get what I can out of the Apache documentation, I've tried the following:
RewriteEngine on
RewriteRule /usr/local/etc/httpd/htdocs/$ /home/
RewriteEngine on
RewriteRule ^usr/local/etc/httpd/htdocs/$ /home/
RewriteEngine on
RewriteRule ^usr/local/etc/httpd/htdocs/$ ^home/
RewriteEngine on
RewriteRule usr/local/etc/httpd/htdocs/$ home/
RewriteEngine on
RewriteRule ^usr/local/etc/httpd/htdocs$ ^home
...and a few other variances with the starting and ending slashes, the carets, etc.
I'm pretty sure I'm getting the regex wrong; either that or I'm messing up somewhere in the syntax of the rule. They're kind of adapted from some rules I'm using in .htaccess files on other sites, and in this case I'm using the httpd.conf file, and I've heard there can be differences. But I haven't been able to find anywhere that defines what those differences might be.
I should add that the way I've been testing this is by loading a PHP script in my browser that prints out the
$DOCUMENT_ROOT, which of course is the full server path to the directory the script is running in. Is there a better way to verify whether or not I'm getting anything to work? Thanks for your help - I appreciate that you're trying to help me figure this out rather than just provide an answer. Once I understand the issues coming into play here, I'm sure it will be a very useful concept to know. (Although, judging by the number of mod_rewrite posts around message boards, it's not an easy concept to grasp!)
Thanks again,
Matthew
A big part of learning mod_rewrite (for me) was the use of the RewriteLog; having it not work doesn't help you much if you can't see what's going on. The RewriteLog will have *tons* of useful information. "Teach a man to fish, and he'll never be hungry again". Trying to teach you to fish, is all. =)
I set up the log file like you suggested. The first time I tried to load a page after setting up the log file directive, I got an error about the file for the log not being found. So apparently the RewriteLog directive was being read. I checked, created the directory and file I'd referenced for the log, and chmod'd to 777 just to make sure it could be written.
After several pageloads, the log file has yet to show a single entry. Obviously I'm doing something wrong. (How was it you put the worm on the hook? ;) )
However, I DID find what you meant (I think) about my mistake:
The first argument to RewriteRule is a URI-path, not a filesystem path. The *second* argument is a a filesystem path *unless* you're issuing a redirect (with the 'R' flag at the end of the RewriteRule).
Based on that, I'm thinking that my problem is that I'm trying to use a filesystem path for both arguments. But, I don't what else to use for the first argument. The directory I want to rewrite (/usr/local/etc/httpd/htdocs) isn't web-accessible. So I don't see how I can use a URI path. (Unless I'm misunderstanding the term - a URI path is http:// right?)
Perhaps I should mention that the htdocs directory is where all my virtual host home directories will be placed. I want the domains for my virtual hosts to be able to use server paths of /home/username instead of /usr/local/etc/httpd/htdocs/username.
I don't mind learning to fish, in fact I appreciate the opportunity, but I must confess to being pretty well lost.
One important question: Do I put the RewriteLog and RewriteRule directives inside the <VirtualHost> </VirtualHost> directives, or can they just go "on their own" anywhere in httpd.conf?
Perhaps I should mention that the htdocs directory is where all my virtual host home directories will be placed. I want the domains for my virtual hosts to be able to use server paths of /home/username instead of /usr/local/etc/httpd/htdocs/username.
Hrm. I may have misunderstood the question. Are you defining 'server paths' as "paths the browser uses to pull up a user page" or "path to the user's files on the filesystem"? If the latter, then I've started out on the wrong path *completely* with this explanation. =)
Are you trying to affect what's seen in the browser, or where the files are on the server's filesystem?
Are you trying to affect what's seen in the browser, or where the files are on the server's filesystem?
Where the files are on the filesystem. The thing is, I can't actually change the directory structure in this case. So I need some way of shortening those long server paths:
/usr/local/etc/httpd/htdocs/user
...to something more reasonable:
/home/user
Then, for instance with scripts that require a server path to the user's document root, they'll be able to use a much shorter path than the first one I showed here.
Basically, I need some way of aliasing, rewriting, or something, so that the /usr/local/etc/httpd/htdocs/ directory can simply be referred to as /home/.
/home/user1/www -> /usr/local/apache/htdocs/user1
/home/user2/www -> /usr/local/apache/htdocs/user2
...but that's because some of the users on that system have shell access, and therefore have their own home directories anyway. =)
Sitz, I know what we discussed about mod_rewrite didn't actually come into play here, but I need to learn mod_rewrite better anyway and I have a feeling your suggestion about the rewrite log will help out a lot!
Thanks again,
Matthew
Now that I got this part of the job finished, I got the idea of taking things one step further. Sure, I've got the softlink set up so the shorter server path can be used, but I ran a simple PHP script:
<?php print $DOCUMENT_ROOT;?>
And, as I knew it would, it printed the long server path. Now, I understand why it uses the long path; my softlink is just a shortcut and naturally wouldn't come into play here. But now I'm wondering, how can I get the $DOCUMENT_ROOT to display as the shorter path as well?
Would this be where mod_rewrite would come in?
Thanks a lot,
Matthew