Page 1 of 1

pass variables to htaccess from php script

Posted: Mon Oct 12, 2009 1:15 am
by gth759k
I just got my htaccess redirect working, but I'd like to make it better

when you log in the url is typically something like this
http://www.mydomain.com/profile.php?id=1234567890

the htaccess file has a redirect like this

Code: Select all

 
RewriteEngine On
RewriteRule ^([A-Za-z0-9-]+)/?$ profile.php?id=$1 [NC,L]
 
now you can reach the same location by typing in
http://www.mydomain.com/1234567890

the problem is I've developed my whole site and database around the user id being a number

so, I'd like to be able to add a row in my user table for usernames

and point the url to this sort of thing
http://www.mydomain.com/username

Is there a way to make the htaccess file redirect this
http://www.mydomain.com/username
to this
http://www.mydomain.com/profile.php?id=1234567890

by making a mysql call that grabs the corresponding user id for the username and redirecting?

Any help would be appreciated. Thanks.

Re: pass variables to htaccess from php script

Posted: Mon Oct 12, 2009 3:51 am
by Weiry
gth759k wrote:by making a mysql call that grabs the corresponding user id for the username and redirecting?
Im not very familiar with htaccess redirects etc, so i may not be able to help a whole lot.
But i believe the query you would need would be something like:

Code: Select all

SELECT `id` FROM `usersTable` WHERE `username` = '{$_GET['username']}'
Then you would do your redirection based upon the user id returned.

I did a quick google search and returned this site: more .htaccess tips and tricks..
Have a look for the section:
"not-so-simple rewriting ... flat links and more"

I think thats what your after.

Re: pass variables to htaccess from php script

Posted: Mon Oct 12, 2009 5:18 am
by markusn00b
A RewriteMap can be used in this case - pay attention to the 'External Rewriting Program' subsection.

Hope this helps,
Mark.

P.S. You will need access to your Apache configuration files to implement this solution.