Mod Rewrite quick help!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bumple
Forum Commoner
Posts: 34
Joined: Thu Jan 08, 2004 4:38 am

Mod Rewrite quick help!

Post by bumple »

Hi all!

I made a user_template.php for displaying any user's information from a database, however, I have a few questions after reading the modrewrite FAQ and the Working with Forms Tutorial.

I want users to come to http://www.domain.com/username to be redirected to http://www.domain.com/user_template.php?user=username.

1) Since my modrewrite won't contain numbers (as it did on the short tutorial at http://codewalkers.com/faq/4/13.html) and just ID's, how would I do it? Would it be something like this in my .htaccess file?:

[quote]
RewriteEngine on
RewriteBase /
RewriteRule ^(<what goes here>+)/$ /user_template.php?user=$1
[/quote]



2) I do have access to my .htaccess file, but it already has a bit of stuff in it. One thing: my current .htaccess is in the /logs directory of my server. Can I make a new .htaccess for the root directory of my server (dont I have to do that?)?


Thanks a lot guys!
-BUMPle.
steve@oeic.net
Forum Newbie
Posts: 8
Joined: Wed Feb 18, 2004 2:47 pm

Post by steve@oeic.net »

([a-z]+[A-Z]+) might work, but I'm not great with regular expressions.
bumple
Forum Commoner
Posts: 34
Joined: Thu Jan 08, 2004 4:38 am

Post by bumple »

that doesn't seem to be working...
-BUMPle.
bumple
Forum Commoner
Posts: 34
Joined: Thu Jan 08, 2004 4:38 am

Post by bumple »

BUMPity!
-BUMPle.
tylerdurden
Forum Commoner
Posts: 66
Joined: Mon Jul 28, 2003 11:52 am
Location: Austria

Post by tylerdurden »

I got it to work like this:

RewriteRule ^user_(.*)$ user_template.php?user=$1

it however requires you to prepend user_ to the actual username like so:

http://www.domain.com/user_username

or use this:

RewriteRule ^user/(.*)$ user_template.php?user=$1

and call it like

http://www.domain.com/user/username

Else I don't know, since you'd have to make sure you can still call normal pages (index.php) and subdirectories. Your regex would have to catch these.
bumple
Forum Commoner
Posts: 34
Joined: Thu Jan 08, 2004 4:38 am

Post by bumple »

I'm getting there...but take for example xanga's website.

If you type in:

http://www.xanga.com/ilovecats


it will automatically redirect you to:

http://www.xanga.com/home.aspx?user=ilovecats


How did they do that?

Thanks!
-BUMPle.
tylerdurden
Forum Commoner
Posts: 66
Joined: Mon Jul 28, 2003 11:52 am
Location: Austria

Post by tylerdurden »

Didn't say it wasn't possible;-)
I overlooked the obvious. Try this:

RewriteRule ^([a-zA-Z0-9_]*)$ user_template.php?user=$1
Post Reply