pass variables to htaccess from php script

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
gth759k
Forum Commoner
Posts: 76
Joined: Mon Jun 15, 2009 3:04 am

pass variables to htaccess from php script

Post 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.
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: pass variables to htaccess from php script

Post 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.
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: pass variables to htaccess from php script

Post 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.
Post Reply