[英]How to make users system example.com/username
如何使用户系统为example.com/username
You need to use mod_rewrite if you are on Apache 如果您在Apache上,则需要使用mod_rewrite
Here is an article on how you would implement it: 这是有关如何实现的文章:
http://articles.sitepoint.com/article/guide-url-rewriting http://articles.sitepoint.com/article/guide-url-rewriting
This answers your question http://articles.sitepoint.com/article/guide-url-rewriting 这回答了您的问题http://articles.sitepoint.com/article/guide-url-rewriting
Edit: Arg Beaten by 2 minutes. 编辑:Arg Beaten 2分钟。 I am slow. 我很慢 oh well here is another link for an alternative method http://www.thyphp.com/friendly-url-without-mod_rewrite.html 哦,这是替代方法的另一个链接, 网址为http://www.thyphp.com/friendly-url-without-mod_rewrite.html
Your web server needs to know how to handle such URLs. 您的Web服务器需要知道如何处理此类URL。 Most web server software has an extension that allows to rewrite requested URLs internally. 大多数Web服务器软件都有一个扩展名,该扩展名允许在内部重写请求的URL。
As for Apache's web server there is mod_rewrite that allows a rule based URL rewriting. 对于Apache的Web服务器,有mod_rewrite ,它允许基于规则的URL重写。 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: 在这种情况下,您可以在.htaccess文件中使用以下规则将此类请求的URL路径内部重写为user.php文件:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^/]+$ user.php [L]
Note that this rule does only add a new way to request /user.php
; 注意,此规则仅添加一种新的请求/user.php
; so it is still possible the request /user.php
directly. 因此仍然有可能直接请求/user.php
。
Within the user.php you can access the requested URI path with $_SERVER['REQUEST_URI']
. 在user.php中,您可以使用$_SERVER['REQUEST_URI']
访问请求的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. 现在,您只需要调整应用程序即可提供正确的URL,因为mod_rewrite只能重写传入的请求,而不能重写传出的响应。
But apart from that, I would rather suggest a different URI design with a distinct prefix like /user/…
. 但是除此之外,我宁愿建议使用不同的URI设计,例如/user/…
。 Otherwise users might choose URLs that conflict with existing ones like /index.html
, /robots.txt
, /sitemap.xml
, etc. 否则,用户可以选择的网址,与像现有的冲突/index.html
, /robots.txt
, /sitemap.xml
等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.