www.matemingler.com
www.matemingler.com

Reputation: 77

Custom urls - giving each user a url

I am building a website and I would like to customize the users' profile so their profile url shares their name. For example, the website domain would be www.example.com and the users' url would be www.example.com/username.

I am assuming this is a convention because I see this all around the web. Is this done by giving each user their own directory and how painstaking would that be?

Upvotes: 3

Views: 2805

Answers (3)

Patrick Kostjens
Patrick Kostjens

Reputation: 5105

You are looking for a RewriteRule. I think the following should do:

RewriteRule ^(.*)$ index.php?username=$1 [NC]

This will convert the displayed url (www.example.com/myusername), to an url with a GET parameter for your index.php. This new url that you can use internally will look like this:

www.example.com/index.php?username=myusername

Update: Here is an extra clarification to answer the questions in your comment:

The RewriteRule above does exactly what you are asking. The user can enter an url like www.example.com/username, which will be internally rewritten (without the user ever noticing it) to www.example.com/index.php?username=myusername. That way you can access the get variable ($_GET["username"]), without the user ever seeing it exists.

Nice tutorials can be found here and here.

Upvotes: 3

m59
m59

Reputation: 43745

I really can't justify using htaccess for something like this. I only use htaccess to route everything through one php file (my root index.php) and let php sort out how to handle the url. For example, you could do this:

$uri = trim($_SERVER['REQUEST_URI'], '/');
$pieces = explode('/', $uri);
$username = $pieces[0];

Then do something with $username.

The way I parse and use my url's is a bit more complicated than this, but it wouldn't be relevant to your question. Just make sure whatever you do is able to account for any possible query strings, etc.

mod-rewrite is not great for performance, so I wouldn't abuse it.

Updated based on your comments.

Here's just a slight expansion on my original code sample:

//.htaccess - route all requests through index.php
RewriteCond %{REQUEST_URI} !\.(png|jpe?g|gif|css|js|html)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php [L]

and this is an example of what you could do in index.php:

$pieces = preg_split('-/-', $_SERVER['REQUEST_URI'], NULL, PREG_SPLIT_NO_EMPTY);
$username = $pieces[0];

include "users/{$username}.php";

Now you could visit mysite.com/myUserNameHere, the request goes to index.php which parses out the username and includes a file for that user.

That takes care of the routing just like you asked. I said that my routing is more complicated, but my use is a lot more complicated, so it isn't relevant. I only warned that my code here doesn't account for a url with a query string attached, like "mysite.com/someUser/?foo=bar". It's a simple process to parse the url without the query string and you should be able to handle that. If you need further assistance parsing the url, then make a post asking about that.

Upvotes: 3

Anonymous
Anonymous

Reputation: 3689

What you mean are not 'real' URIs, but are rewrites. So with .htaccess you can redirect the viewing user from a regular URI like example.com/user to example.com/users.php?name=user without him seeing it.

It is kind of a convention, as it's useful to make URIs more readable, it also can let Google give a better ranking for that page as there might be keywords in the URI and the URI is shorter, so the keywords stand out better.

Here's a basic rewrite, which is to place in your .htaccess file somewhere on your server (mostly in the root so it can control all subfolders from there on):

 RewriteRule ^(.*)$ users.php?name=$1 [NC]

Upvotes: 0

Related Questions