简体   繁体   中英

How to make users system example.com/username

如何使用户系统为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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM