.htaccess RewriteRule map URL to another

hi,

I’ve developed a web program and let say the main url is http://domain.com

I will be distributing different URLs for different users, say http://domain.com/user1
and http://domain.com/user2

however, both this URLs will be pointing to the base code at http://domain.com. depending on whether is user1 or user2, the system configuration will extract from the respective database.

my question is, how do i use mod_rewrite to map the different urls to the main url with transparency.

I’ve managed to do it with alias in httpd.conf. e.g:
alias /user1 /var/www/html/myproject
alias /user2 /var/www/html/myproject

however, if i were to do this, i would have to restart apache every time i’ve got a new user. so i’ve decided to use .htaccess instead but realised that alias is not availble for it.

I’ve tried with rewriterule but with no success.

Please advise. thank you

What a lot of popular packages do (like WP), and what it seems like you’re doing is directing your users through to a main page for processing.

So, brush up on your regular expressions because that’s what mod_rewrite uses (there are plenty of good tutorials stickied at the top of this thread) and dig in.

First you’ll need to start the engines:

RewriteEngine on

Then apply some conditions to make sure you’re not actually requesting a real file or directory.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %(REQUEST_FILENAME} !-d

Then, since your username is likely a set of characters and numbers this can be expressed in a regular expression [a-zA-Z0-9], so when that’s added.

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %(REQUEST_FILENAME} !-d

RewriteRule ^[a-zA-Z0-9]/?$ load_user.php [L]

Or

Rewrite Rule ^([a-zA-Z0-9])/?$ load_user.php?username=$1 [L]

Keep in mind that the caveat there is if users create their usernames the same names as your directories… so really you’d want to make them have a url like /u/[username]

EXCELLENT, Jack!

Regards,

DK

hey Jack, tks a lot for the code.

i place htaccess file in the main directory of the base code.
i tried access http://domain.com/user1 and it gave a 404 error

im sure mod_rewrite is enabled as i tried the following and it works:
RewriteEngine On
RewriteRule (.*) http://www.google.com/

############
updates
############
i hard-coded the directory and now it works:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %(REQUEST_FILENAME} !-d
RewriteRule ^user1/?$ / [L]

so now http://domain.com/user1 works
but if i were to access other part of the program, e.g http://domain.com/user1/login/index.php, it did not point to http://domain.com/login/index.php but instead throw a 404 error

just for clarification, im not using user1 as a query string to define the respective database (domain.com?username=user1). in my base code at http://domain.com, it has a config file. when user access domain.com/user1, im gonna use this config file to extract “user1” from the url to pick out the respective database

Hey Jack,

I managed to find the solution.

What I did:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %(REQUEST_FILENAME} !-d
RewriteRule [1](.*)$ /$1 [L]

Jack, thanks alot for this again. this solution has pretty much leave my mind at peace at least for a while.


  1. a-zA-Z0-9 ↩︎

Ah, that’s because there were two mistakes. Accidentally did (REQUEST_FILENAME} and [a-zA-Z0-9] was only catching 1 letter. So I forgot to add a quantifier.

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^[a-zA-Z0-9]+/?$ load_user.php [L]

As for the solution you posted, in english it’s looking for 1 character that’s normal and then 0 or more characters of anything.

http://www.domain.com/d/omain/user would work in that scenario.