Forum Moderators: phranque
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.
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?
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
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]
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?