Forum Moderators: phranque

Message Too Old, No Replies

I have a question maybe an apache veteran can guide me?

concerning mod rewrite or alias'

         

BobbyBudds

4:31 am on Apr 15, 2005 (gmt 0)

10+ Year Member



Hello. First off I'm happy to find this forum! I'm very new to the world of apache because I had never used it before. I just started using php instead of ASP, because of open source. I have created a web site which users can sign up on and have there own little personal web page through the site. Now here is the problem I ran into.

In my root directory I have created a directory called Users. When someone creates an account on my website there is a folder created in the Users directory. The folder is the name of the User. So I have Users/[UserName]. Now I want to offer the users there own URL for there webpage. Right now there Url would be

[websitename.com...]
example [websitename.com...]

What I would like is to have the URL
[websitename.come...] while still keeping the directory structure in my webserver
/users/[username]

Is this possible? I'm sure it is but I'm not sure where to start because the usernames are all dynamic. If a veteran can point me in the right direction this apache newb would greatly appreciate it. Thank you.

sitz

1:23 pm on Apr 15, 2005 (gmt 0)

10+ Year Member



This is actually fairly easy, with a caveat. The basic version looks like this:

RewriteEngine on
RewriteRule ^/(.*) /Users/$1 [L]

Caveat:

Let's assume your site is www.example.com. The above assumes that /all/ files/directories under [example.com...] are user directories. If, for instance, you have 'www.example.com/index.html', the above would Rewrite it to /Users/index.html, which would, I'm guessing, 404. To make things a little more flexible:


RewriteEngine on
#
# Capture the (potential) username in %1
RewriteCond %{REQUEST_URI} ^/([^/]+)/
#
# Check to see if a directory for that username
# exists on the filesystem
RewriteCond /Users/%1 -d
#
# If so, Rewrite; we could capture the username with
# parens in the Rule and use $1 instead of %1, but
# why waste the CPU cycles when %1 already has what we
# need?
RewriteRule ^.* /Users/%1 [L]

Does this help?

Double_Dark

1:24 pm on Apr 15, 2005 (gmt 0)

10+ Year Member



I am admitedly a neophyte where apache is concerned but, I believe that you can do this with mod_rewrite. Something along the lines of

RewriteEngine on
RewriteCond /users/%{REQUEST_FILENAME} -f [NC]
RewriteRule ^(.+) /users/$1 [L]

BobbyBudds

8:42 pm on Apr 15, 2005 (gmt 0)

10+ Year Member



Thank you so much for the help. I haven't got it working yet still, but this helps me on my way. I've been reading through the apache documentation for about 5 hours now, and still can't get it. I have tried to use this in the .htaccess files? Is that where this code should be put. I have also enabled the re-write module in the httpd.conf file. I'm a total newb, and the documentation is pretty hard to dig through. I'll keep trying though if it takes me all night.

BobbyBudds

11:52 pm on Apr 15, 2005 (gmt 0)

10+ Year Member



Ok, so I've been working on this for quite a while now. I've learned alot about apache in the process so far. I have another quick question. Where is the better place for this code? In the httpd.conf file or in the .htaccess file in the root dir? Thank you.

gliff

12:48 am on Apr 16, 2005 (gmt 0)

10+ Year Member




I have another quick question. Where is the better place for this code? In the httpd.conf file or in the .htaccess file

In the httpd.conf file, if you're allowed. Using mod_rewrite in .htaccess files eats up extra server resources.

sitz

2:24 am on Apr 16, 2005 (gmt 0)

10+ Year Member



Yep; this fact (and some of the details as to why) are documented on the mod_rewrite page [httpd.apache.org] at apache.org (.htaccess == 'per directory').

Note also that a VERY good way to see what mod_rewrite is doing is to use the following config directives in httpd.conf:


RewriteLog /path/to/rewrite.log
RewriteLogLevel 9

You should NOT use these directives on a production machine that receives any significant traffic; the output from these directives is copious. Use these on a test machine to get the your RewriteRules working properly, then copy the rules (minus the Log directives) to your production host.

BobbyBudds

8:22 pm on Apr 16, 2005 (gmt 0)

10+ Year Member



Thx for the help guys. I really appreciate it. That test log is awesome!

BobbyBudds

9:32 pm on Apr 16, 2005 (gmt 0)

10+ Year Member



Sitz. One last question here for you. First off you've been very helpful and thank you. Ok so this is what the log is outputting when i try to access anything.com/dan

127.0.0.1 - - [16/Apr/2005:15:23:52 --0600] [localhost/sid#26bd88][rid#c6b048/initial] (2) init rewrite engine with requested uri /dan
127.0.0.1 - - [16/Apr/2005:15:23:52 --0600] [localhost/sid#26bd88][rid#c6b048/initial] (3) applying pattern '^.*' to uri '/dan'
127.0.0.1 - - [16/Apr/2005:15:23:52 --0600] [localhost/sid#26bd88][rid#c6b048/initial] (4) RewriteCond: input='/dan' pattern='^/([^/]+)/' => not-matched
127.0.0.1 - - [16/Apr/2005:15:23:52 --0600] [localhost/sid#26bd88][rid#c6b048/initial] (1) pass through /dan

now in my webserver the directory structure is webapps/users/dan

Just to be clear I want to type in [anything.com...] rather then [anything.com...]

When I type [anything.com...] <- trailing slash the output i get is

127.0.0.1 - - [16/Apr/2005:15:41:14 --0600] [localhost/sid#26bd88][rid#c96140/initial] (2) init rewrite engine with requested uri /dan/
127.0.0.1 - - [16/Apr/2005:15:41:14 --0600] [localhost/sid#26bd88][rid#c96140/initial] (3) applying pattern '^.*' to uri '/dan/'
127.0.0.1 - - [16/Apr/2005:15:41:14 --0600] [localhost/sid#26bd88][rid#c96140/initial] (4) RewriteCond: input='/dan/' pattern='^/([^/]+)/' => matched
127.0.0.1 - - [16/Apr/2005:15:41:14 --0600] [localhost/sid#26bd88][rid#c96140/initial] (4) RewriteCond: input='/Users/dan' pattern='-d' => not-matched
127.0.0.1 - - [16/Apr/2005:15:41:14 --0600] [localhost/sid#26bd88][rid#c96140/initial] (1) pass through /dan/

It seems when it tries to match and fail it's missing a trailing slash in the log?

Just to be clear I want to type in [anything.com...] rather then [anything.com...]
so it bypasses the user directory. It isn't matching the pattern when it looks for dan. Am I doing something wrong, or is it possible that the code is incorrect. Don't get me wrong I don't want you to just do it for me, but i'm just curious how confident you are that the code should work. If the code should work, then i must be doing something else wrong. The code is in the config section of the httpd.conf file. Let me say again how much I appreciate your help so far. Thanks.

One more thing I'm just using it on the local machine so i'm using localhost/dan. This shouldn't mess anything up should it? Thanks.

[edited by: BobbyBudds at 9:44 pm (utc) on April 16, 2005]

jdMorgan

9:42 pm on Apr 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The pattern ^/([^.]+)/ shown in your rewritelog would not match "/dan". It would require "/dan/".

You might try ^/([^/]+)/? or just ^/([^/]+) depending on your needs.

Jim

BobbyBudds

9:44 pm on Apr 16, 2005 (gmt 0)

10+ Year Member



thx jd i'll try it out.

BobbyBudds

10:48 pm on Apr 16, 2005 (gmt 0)

10+ Year Member



Ok I've been messing around with this changing the test expression, and a few other things and trying it. Now the thing that gets me the most here is in my log I get

127.0.0.1 - - [16/Apr/2005:16:32:11 --0600] [localhost/sid#26bd88][rid#cace98/initial] (3) applying pattern '^.*' to uri '/dan/'
127.0.0.1 - - [16/Apr/2005:16:32:11 --0600] [localhost/sid#26bd88][rid#cace98/initial] (4) RewriteCond: input='/dan/' pattern='^/([^/]+)/' => matched
127.0.0.1 - - [16/Apr/2005:16:32:11 --0600] [localhost/sid#26bd88][rid#cace98/initial] (4) RewriteCond: input='/users/dan/' pattern='-d' => not-matched

127.0.0.1 - - [16/Apr/2005:16:33:22 --0600] [localhost/sid#26bd88][rid#bff840/initial] (3) applying pattern '^.*' to uri '/dan/'
127.0.0.1 - - [16/Apr/2005:16:33:22 --0600] [localhost/sid#26bd88][rid#bff840/initial] (4) RewriteCond: input='/dan/' pattern='^/([^/]+)/' => matched
127.0.0.1 - - [16/Apr/2005:16:33:22 --0600] [localhost/sid#26bd88][rid#bff840/initial] (4) RewriteCond: input='/users/dan' pattern='-d' => not-matched

That is 2 seperate examples. Why is the RewriteCond: pattern='-d' line "not-matching"? Could my path be incorrect? My directory structure is webapps/users/dan/index.html

webapps is the webroot, and in the browser i can type localhost/users/dan/ and it will bring me to the page. So why wouldn't this be matching?

sitz

11:33 pm on Apr 16, 2005 (gmt 0)

10+ Year Member



when using -d to check for the existence of a directory, you're dealing with the filesystem, not the web root; thus, it expects the absolute filesystem path:

RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/webapps/users/dan/ -d
(... rewrite rules here ...)