想做一个简单的 url 缩短器
我想创建一个简单的 URL 缩短程序,它可以执行以下操作:
其中代码类似于 a476u(由字母和数字组成的随机字符串)。我唯一的问题是从网址读取代码。我希望所有请求都转到index.php,它会在其中读取网址并获取代码。但这样的 URL 通常会指向一个文件夹(其中 a476u 是一个文件夹)。
如何在不输入文件名的情况下向文件而不是文件夹发出这样的请求?
I want to create a simple URL shortner that would do the following:
where code would be something like a476u (a random string of letters and numbers). My only problem is reading the code from the url. I want all requests to go to index.php where it would read the url and get the code. But a URL like that would normally point to a folder (where a476u is a folder).
How can I make a request like that go to a file rather then a folder without putting the file name?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对我来说听起来像是一个
.htaccess
文件...然后,请随意在
Sounds like an
.htaccess
file to me...Then, feel free to try it on this site.
根据 URL 将请求定向到 PHP 是 Web 服务器的工作,而不是 PHP 本身的工作。如果您使用 Apache 作为 Web 服务器,则可以使用 mod_rewrite 来重写调用index.php 的URL。
Directing requests to PHP based on the URL is the job of the webserver, not PHP itself. If you are using Apache as a webserver, you can use mod_rewrite to rewrite the URL such that you index.php is invoked.
我认为你的意思是你希望“代码”被视为index.php的上下文(参数),而不是像文件或目录这样的资产。这是您需要在网络服务器中配置的内容。您可能希望将某些
/paths
解析为文件或目录,因此您必须对它们进行例外处理。其他所有内容都解析为index.php。然后在你的index.php中获取url并解析出路径,即代码。I think what you mean is that you want "code" to be treated as a context (argument) to index.php as opposed to an asset like a file or directory. This is something you need to configure in your webserver. You may have some
/paths
that you want to be resolved as files or directories, so you must make exceptions for these. Everything else resolves to index.php. Then in your index.php get the url and parse out the path, ie the code.检查这里
http://net.tutsplus.com/教程/其他/运行您自己的tinyurl-service-with-phurl/
Check here
http://net.tutsplus.com/tutorials/other/run-your-own-tinyurl-service-with-phurl/