mod_rewrite

Modeps

Lifer
Oct 24, 2000
17,254
44
91
I presume that I need to use Mod_rewritemod_rewrite to accomplish this, and if not, maybe someone can point me in the right direction.

I've got a PHP/MySQL website. Almost all of the content is served off of url variables like id=1

What I'd like to do is instead of having something like:

http://foo.com/user.php?id=1

is to allow for:

http://foo.com/user/modeps

Is there a quick solution? I could use a hand, as I've never done anyting with mod_rewrite before.
 

troytime

Golden Member
Jan 3, 2006
1,996
1
0
in your .htaccess

RewriteEngine On
RewriteCond %{HTTP_HOST} ^foo\.com
RewriteRule ^user/(.*)?$ user.php?userName=$1 [L]




then in your user.php, change the query to use $_GET['userName'] instead of the userId.
you should also validate it before querying for it, url decode, addslashes, whatever floats your boat :)
 

Modeps

Lifer
Oct 24, 2000
17,254
44
91
hmm, seems easy enough. Thanks for that. I'll give it a go and let you know how I make out.

~Jim
 

troytime

Golden Member
Jan 3, 2006
1,996
1
0
i think the 2nd line is unnecessary, but it was in my htaccess, so i pasted it here too :)

if you get a server error, you'll need to enable mod_rewrite in httpd.conf
(just find the #LoadModule rewrite_module modules/mod_rewrite.so and remove the #)
 

Modeps

Lifer
Oct 24, 2000
17,254
44
91
Awesome, it worked wonderfully... thank you for your help :)
Much easier than I thought it would be too. :D
 

troytime

Golden Member
Jan 3, 2006
1,996
1
0
no prob
if you need more than one variable, you can just do


RewriteRule ^fakepath/(.*)/(.*)?$ realpath/realscript.php?var1=$1&var2=$2