cult3

How to make vanity URLs using PHP, .htaccess and MySQL

Nov 16, 2011

Table of contents:

  1. What are we going to need?
  2. The .htaccess file
  3. The profile.php file

Vanity URLs have become a staple part of any good online service and are a really good way of helping increase engagement and social sharing of your web app or website. For those of you that don’t know, a vanity URL is usually a direct link to a users profile in the following form - twitter.com/philipbrown.

Here is a quick and easy tutorial for creating vanity URLs for your next project using PHP and .htaccess and an explanation of how those profiles are routed to the correct user in your database using MySQL.

What are we going to need?

Right, to make vanity URLs, you’re going to need the following items in some shape or form.

  • .htaccess
  • profile.php
  • MySQL database

And here is a quick overview of how this process is going to work.

  1. A user will go to your-url.com/username
  2. The .htaccess file will recognise this URL as a vanity URL
  3. The .htaccess file will then pull the profile.php file
  4. The profile.php file will then look up the user based on the URL
  5. The profile.php file will then display the correct user’s profile page

Still with me? Good! Now let’s make what you need.

The .htaccess file

Here is the code that you will need to put in your .htaccess file. At this point I should probably explain what an .htaccess file is…

Basically, the .htaccess file in our case is going to be used to detect and redirect URLs. .htaccess files do a lot, much more than the scope of this tutorial, but that’s all you really need to know at the minute. Have a look at The Ultimate Guide to .htaccess files if you want a more detailed explanation of what an .htaccess file is capable of.

So here’s the code you are going to need to put into your .htaccess file. For a full explanation of the mod_rewrite function, click here.

RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC]
RewriteCond %{REQUEST_FILENAME} >""
RewriteRule ^([^\.]+)$ profile.php?user=$1 [L]</pre>

And here’s an explanation of what each line does.

RewriteEngine on

Our first line turns on the rewrite engine.

RewriteCond %{REQUEST_FILENAME}.php -f

The line above checks to see if the request is a file

RewriteRule ^([^\.]+)$ $1.php [NC]

This line turns adds the .php extension. So for example if you URL is /friends, the friends.php file would be called.

RewriteCond %{REQUEST_FILENAME} >""
RewriteRule ^([^\.]+)$ profile.php?user=$1 [L]</pre>

The final two lines pick up the url when it is not a file name, for example, your-url.com/philipbrown and routes it to profile.php?user=philipbrown.

Now that we have the URL in the correct form we can use the selected username to pull the right profile information.

The profile.php file

Next we’re going to create the profile.php file. This file will interpret the URL, pick out the name from the URL and find the user that matches that name.

Here is the first bit of code you will need:

$getName = explode("/", $_SERVER["REQUEST_URI"]);

This returns an array of strings broken up by the “/“.

Next we need to query the database to find the correct user. This step is assuming that you’ve required your user to pick a vanity URL or a username to be used as the vanity URL.

$result = mysql_query("SELECT * FROM user WHERE url='$getName[3]'");
$num_rows = mysql_num_rows($result);

In the above query, we are simply selecting the user where the URL field matches what was in the URL.

Next we write a little catch to check to make sure a user was found. If people enter your URL then a name that is not a user in your database you will want to send them to your 404 page.

if ($num_rows == 0) {
    header("Location: 404");
}

Now that you’ve caught any instances that are not in your database you can use the returned data to populate your profile page.

It’s as simple as that!

And here’s an example of a profile page that is using a vanity URL.

If you have an questions, feel free to leave a comment!

Philip Brown

@philipbrown

© Yellow Flag Ltd 2024.