scireon
scireon

Reputation: 395

How can I create a dynamic url with your username?

I want to create a dynamic url such that when I log in from my website my username will be shown on the url. For example, say I log in with:

http://example-website.com

with my username myname. The url should become

http://example-website.com/myname

but actually the web page is loginsuccess.php with an alias of myname which is my username

How can I do this?

Upvotes: 1

Views: 2890

Answers (2)

Rakesh Sankar
Rakesh Sankar

Reputation: 9415

Quick Tips:

  • Store unique username in a table.
  • Enable rewrite module in your http-server (here's a tutorial on that).
  • Rewrite the URL http://example.com/username to a route.php (or something similar).
  • Route.php should read the URL and extract the username and cross-verify it with the appropriate table.
  • If a match is found then show the appropriate page otherwise 404.
  • DONE!

For a thorough explanation, see Facebook Like Custom Profile URL PHP.

Upvotes: 1

classicjonesynz
classicjonesynz

Reputation: 4042

This is actually pretty easy with .htaccess and RewriteEngine. In the example below I'll be using some really simple (.*) regex, which is just a wildcard so that everything will be accepted (a-zA-z0-9).

/username prefixed with profile

RewriteEngine on
RewriteRule ^profile/(.*) user_profile.php?username=$1
ErrorDocument 404 /pathtofile/404.html

Result: http://www.example-website.com/profile/john-smith

Working with just username, this option will require some sort of Routing class.

RewriteEngine on
RewriteRule ^(.*) user_profile.php?username=$1
ErrorDocument 404 /pathtofile/404.html

Result: http://www.example-website.com/john-smith

Using RewriteEngine with Suffix auth

RewriteEngine on
RewriteRule ^(.*)/auth auth_user.php?username=$1&q=auth
ErrorDocument 404 /pathtofile/404.html

Result: http://www.example-website.com/john-smith/auth

Upvotes: 5

Related Questions