KUNAL AGRAWAL
KUNAL AGRAWAL

Reputation: 123

how to give each user a unique url

favorite 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.

at present when i do a test sign it looks like test/admin/index.php i want to rewrite it as test/admin/testuser

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: 1

Views: 1051

Answers (2)

Fabio Gonzaga
Fabio Gonzaga

Reputation: 58

In your htaccess you can try this:

RewriteRule ^/([a-zA-Z0-9_-]+)$ index.php?user=$1 [QSA,L]

And in your php:

if(isset($_GET['user'])) {
    // code for frontpage
} else {
    $username = $_GET['user'];
}

I hope this help you

Upvotes: 0

Martin Schlagnitweit
Martin Schlagnitweit

Reputation: 2101

Usually this is done by routing. You redirect all requests to the index.php file with .htaccess

Your setup could look like this:

.htaccess

RewriteEngine on
RewriteBase /
RewriteCond  %{REQUEST_FILENAME} !-f
RewriteCond  %{REQUEST_FILENAME} !-d
RewriteRule  ^(.*)$ index.php?param=$1 [QSA,L]

index.php

<?php

if(empty($_GET['param'])) {
    // code for frontpage
} else {
    $username = $_GET['param'];
}

?>    

Upvotes: 1

Related Questions