开发者

Custom URL with PHP

I have a small question to ask. Is it possible, via php or hta开发者_Go百科ccess, 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.


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.


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.


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


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

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜