开发者

want to do a simple url shortner

开发者 https://www.devze.com 2023-02-20 13:42 出处:网络
I want to create a simple URL shortner that would do the following: http://example.com/code where code would be something like a476u (a random string of letters and numbers).My only problem is readi

I want to create a simple URL shortner that would do the following:

http://example.com/code

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?


Sounds like an .htaccess file to me...

RewriteRule ([a-zA-Z0-9]{5}) index.php?code=$1

Then, feel free to try it on this site.


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.


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.


Check here http://net.tutsplus.com/tutorials/other/run-your-own-tinyurl-service-with-phurl/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号