How to make users syste开发者_运维百科m example.com/username
You need to use mod_rewrite if you are on Apache
Here is an article on how you would implement it:
http://articles.sitepoint.com/article/guide-url-rewriting
This answers your question http://articles.sitepoint.com/article/guide-url-rewriting
Edit: Arg Beaten by 2 minutes. I am slow. oh well here is another link for an alternative method http://www.thyphp.com/friendly-url-without-mod_rewrite.html
Your web server needs to know how to handle such URLs. Most web server software has an extension that allows to rewrite requested URLs internally.
As for Apache’s web server there is mod_rewrite that allows a rule based URL rewriting. In you case you could use the following rule in the .htaccess file to rewrite such requested URL paths internally to a user.php file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^/]+$ user.php [L]
Note that this rule does only add a new way to request /user.php
; so it is still possible the request /user.php
directly.
Within the user.php you can access the requested URI path with $_SERVER['REQUEST_URI']
. To extract the user name, you could use the following:
$_SERVER['REQUEST_URI_PATH'] = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$username = substr($_SERVER['REQUEST_URI_PATH'], 1);
Now you just need to adjust your application to serve the proper URLs as mod_rewrite can only rewrite incoming requests and not outgoing responses.
But apart from that, I would rather suggest a different URI design with a distinct prefix like /user/…
. Otherwise users might choose URLs that conflict with existing ones like /index.html
, /robots.txt
, /sitemap.xml
, etc.
精彩评论