4

I have a small question to ask. Is it possible, via php or htaccess, to change a url like: miodominio.com/users.php?idu=x into something like miodominio.com/username ?

I want it to be Facebook style...Where "username" is the username chosen by idu = x.

1
  • it's good form to "accept" an answer that solves your problem. You can do this by clicking the "tick" beside the answer that you choose. Commented Nov 15, 2011 at 15:49

5 Answers 5

4

There are multiple ways to solve this problem, but here's one that always suits my needs.

Guide all your URL requests through the index.php first and resolve the request in your PHP code second.

1) Use an .htaccess file to direct all URL's through index.php. You'll find one way here by the CodeIgniter framework and a more advanced explanation here. I recommend the CodeIgniter .htaccess guide first if you're inexperienced with .htaccess.

2) Second, use the $_SERVER variable in PHP to extract the URL. Probably with the help of the $_SERVER['REQUEST_URI'], you'll find '/username/' which you can then use to extract the user's data and serve it to them.

Good luck and beware of URL injections using this method.

Sign up to request clarification or add additional context in comments.

Comments

1

You need to use apache's mod_rewrite for this. It can translate miodominio.com/username to miodominio.com/users.php?idu=x. There are some good guides about this which are easy to find with Google.

Comments

0

You can try to use this mod_rewrite pattern (add it to the .htaccess):

RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ users.php?idu=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ users.php?idu=$1

Comments

0

you have to write a clean URL in your .htaccess file like :

RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)/$ users.php?idu=$1

Comments

0

Put the following in your .htaccess

RewriteEngine on
RewriteRule ^([a-z0-9_-]+)/?$ /users.php?idu=$1 [NC]

The [NC] will make it case-insensitive, if you accept only lowercase username, remove the [NC] from the last.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.