简体   繁体   English

如何使用户系统为example.com/username

[英]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.

相关问题 如何制作example.com/eat load example.com/eat.php? - how to make example.com/eat load example.com/eat.php? 如何让所有 example.com 页面重定向到 example.com/smth - How can I make all example.com pages redirect to example.com/smth htaccess将example.com/user.php?id=username重写为example.com/username - htaccess rewrite example.com/user.php?id=username to example.com/username 如何使用.htaccess将http://example.com/test.php设置为http://example.com/index.php?do=test? - How to make http://example.com/test.php to http://example.com/index.php?do=test using .htaccess? 将example.com/section.php?username=xyz重写为example.com/xyz/section - Rewriting example.com/section.php?username=xyz to example.com/xyz/section 如何在Symfony中为example.com/username创建路由并保留所有旧URL - How to create route for example.com/username in Symfony and preserve all old URLs 当有人转到 url https://example.com/{username} 时,我想从 db 获取并显示页面上的用户信息 - I want to fetch and display users info on page from db when someone goes to the url https://example.com/{username} 如何使用example.com/index.php?ref=origin跟踪用户来源PHP - How to track users origin PHP by using example.com/index.php?ref=origin 如何处理从example.com/search/?s=some到example.com/search/some的URL - How to manipulate the URL from example.com/search/?s=some to example.com/search/some 如何将example.com/index.php/lib更改为example.com/lib? - How to change example.com/index.php/lib to example.com/lib?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM