Forum Moderators: phranque

Message Too Old, No Replies

Use shorter server paths, without changing directory structure

What options are available?

         

MatthewHSE

1:43 pm on Mar 24, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've got a reseller hosting account on a shared host. I do have access to my own httpd.conf file. The server paths to my virtual host home directories are along these lines:

/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?

sitz

11:39 pm on Mar 24, 2005 (gmt 0)

10+ Year Member



The short answer is 'yes'. mod_rewrite allows you to rewrite any URL into any other URL (one way or another). The forum charter has links to the mod_rewrite documentation, which should get your started in the right direction. For more complex regexes, you should read up on regular expression syntax; Apache-1.x uses POSIX regexes (documented in regex(5) on Solaris and regex(7) on Linux) and Apache 2 uses Perl-style regexes. If you run into problems, give a shout. =)

MatthewHSE

1:03 pm on Mar 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks sitz, I hadn't thought of using mod_rewrite in this instance. Unfortunately, I'm a know-nothing as far as mod_rewrite is concerned. I've written a couple basic rewrite rules based closely on examples I've seen, but I can't seem to figure this one out.

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

sitz

3:23 pm on Mar 25, 2005 (gmt 0)

10+ Year Member



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). I'm not a huge fan of supplying answers to problems (although I have a tendency to fall into the trap of doing so if I find the problem interesting enough); If all you're doing is a simple Rewrite from $a -> $b, and it should apply to *every* request for a particular path, RewriteCond shouldn't really enter into the equation. When incoming URI it passed to a RewriteRule, it needs to match the regular expression on the left side of the Rule, or the Rule is skipped.

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.

MatthewHSE

4:05 pm on Mar 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there,

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

sitz

7:51 pm on Mar 25, 2005 (gmt 0)

10+ Year Member



If you can set up a RewriteLog (which shouldn't be an issue if the host is not production), that should give you what you need. There *is* one obvious (well, to me, but I've been doing this for a bit) problem with your Rule; at the risk of sounding pompous, the answer's in the first part of my last post.

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. =)

MatthewHSE

8:31 pm on Mar 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm afraide that, as a fisherman, I'm going to be the type who has to ask what a pole is, what's a line, what's the hook for, and what's fishing for that matter? ;)

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?

jamie

10:00 pm on Mar 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



matthew ;), i know how frustrating it can be! been there so many times.

the rewrite log goes in the virtualhost directive. make the path to it in the same folder where all your apache logs go: something like /var/log/apache/rewrite.log

good luck

sitz

4:15 am on Mar 26, 2005 (gmt 0)

10+ Year Member



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?

MatthewHSE

1:48 pm on Mar 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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/.

jamie

2:08 pm on Mar 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



hi matthew,

maybe a soft link would be the best idea

ln -s /usr/local/etc/httpd/htdocs /home

sitz

3:29 pm on Mar 26, 2005 (gmt 0)

10+ Year Member



Symlinks would probably be the way to go, then, yes. On one of the servers I run, I have a layout like this:

/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. =)

MatthewHSE

10:31 pm on Mar 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks guys. I'm assuming that softlink/symlink thing is a SSH command, so my last (?) question here is where I run that command from, and if/how I can "undo" it later?

Thanks for all your help - I really appreciate it.

sitz

10:35 pm on Mar 28, 2005 (gmt 0)

10+ Year Member



check the ln(1) manpage. =)

MatthewHSE

7:41 pm on Apr 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks guys, I finally got it. I was initially confused because I tried to run Jamie's example command from my root directory, which didn't give the results I wanted. But after researching, I realized I needed to run the command from where I wanted the link to point to. So I gave it a try, it worked, and I'm happy now!

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

MatthewHSE

1:43 pm on Apr 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey there,

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

MatthewHSE

1:51 pm on Apr 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, wouldn't you know it, thirty seconds after posting I thought to change the DocumentRoot in the VirtualHost directive to use my shortcut. I made that change, and got the desired result. Why must these things always come to my mind just moments too late? ;)

jamie

9:35 pm on Apr 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



glad you solved it matthew, i have had many eureka moments like that too - often the mere fact of writing it down will help.