4

On my site, user profiles can be reached by the url www.example.com/user/profile.php?uid=3 I want to make it easier for users to reach their profile by simply requesting for www.example.com/username

Each users has a username that cannot change. How can I do this?

Here is my current .htaccess file

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
# If the request is not for a valid link
RewriteCond %{REQUEST_FILENAME} !-l
# finally this is your rewrite rule
RewriteRule ^blog/(.+)$ blog.php/$1 [L,NC]
RewriteRule ^(.+)$ user_page/profile.php?uid=$1 [L,QSA]
anubhava
  • 761,203
  • 64
  • 569
  • 643
Chibuzo
  • 6,112
  • 3
  • 29
  • 51

2 Answers2

7

Use this code in your .htaccess under DOCUMENT_ROOT:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d
# If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f
# If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
# do not do anything
RewriteRule ^ - [L]

# forward /blog/foo to blog.php/foo
RewriteRule ^blog/(.+)$ blog.php/$1 [L,NC]

# forward /john to user_page/profile.php?name=john
RewriteRule ^((?!blog/).+)$ user_page/profile.php?name=$1 [L,QSA,NC]

Now inside profile.php you can translate $_GET['name'] to $uid by looking up user's name into a database table.

Olivier Pons
  • 15,363
  • 26
  • 117
  • 213
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • This will redirect him to `www.example.com/user/profile.php?uid=username`. But there is no way to find the user's id from the username via mod_rewrite, so this is of course the correct approach. ;-) – Basti Mar 26 '12 at 14:14
  • Thanks for the quick reply, However I already have this line in my .htaccess file - RewriteRule ^blog/(.+)$ blog.php/$1 [L,NC], it seems to be causing a problem. Please what do I do? – Chibuzo Mar 26 '12 at 14:16
  • Using mod_rewrite you can have this rule: `RewriteRule ^(.+)$ user/profile.php?name=$1 [L,QSA]` and then inside `profile.php` you can translate `$_GET['name']` to `$uid` by looking up name into a database table. – anubhava Mar 26 '12 at 14:30
  • @Chibuzo: What you mean by blog.php/$1 ? – Afshin Mehrabani Mar 26 '12 at 14:31
  • @anubhava, translating username to uid is not a problem, but as i stated above, I now have two different rules on the .htaccess file and they seem not to cooperate. If I comment one the other will work and vice versa. please how do I fix this? Thanks – Chibuzo Mar 26 '12 at 14:38
  • @Afshin, it's another rewrite rule in my .htaccess file for another page. – Chibuzo Mar 26 '12 at 14:40
  • @Chibuzo: If you post your current .htaccess file in your question I can suggest you how to make both rules working for you. – anubhava Mar 26 '12 at 14:41
  • @Chibuzo: Please check my edited answer where I have combined both of your rules. – anubhava Mar 26 '12 at 14:58
  • @anubhava am I correct in understanding that this solution must be modified to make other scripts accessible in addition to profile.php? For example, if there is a script `site.com/example.php`, does the last rewrite rule redirect this to `site.com/user_page/profile.php?name=example.php`? (same with `.gif` or other file extensions). It seems like a number of exceptions must be provided for to prevent this (or if the username cannot have `.` in it, just do not redirect if `.` exists) – jela Jun 27 '12 at 13:35
  • 1
    @jela: That's not correct since my first rule is avoiding any redirect/forward for any physical file, link or directory. – anubhava Jun 27 '12 at 16:26
2

If you have Apache you should mod_rewrite module.

Create a .htaccess file and upload it on Apache Document Root and put this code:

RewriteEngine on
RewriteRule ^(.+)$ user/profile.php?username=$1

This will pass the username to user/profile.php with username parameter and you can get the username in profile.php and then make your SQL query to get the user profile.

Afshin Mehrabani
  • 33,262
  • 29
  • 136
  • 201
  • 1
    Please elaborate on this answer to improve it. The question is already tagged `mod-rewrite` so the user is looking for a specific answer rather than a general one. – JohnP Mar 26 '12 at 14:09
  • Please look at my comment to the first answer. Thank you – Chibuzo Mar 26 '12 at 14:25