开发者

Create redirect URL

Here is the real case, in the NewsNow.co.uk, there are many link of uptodate news from thousands of website. And the example for one of the news url:

http://newsnow.co.uk/A/471722742?-19721

all the news url are formated like that, but when we click it, we will be brought to the real url, f开发者_JS百科or ex:

http://www.abcactionnewsx.com/dpp/news/state/bla-bla

anyone know how to achieve this efficiently ?


Store a table of 'internal' paths (the 'newsnow' urls) and the 'destination' urls in a database of some sort; sqlite3 would be a fine choice for smaller applcations.

You could hash the 'internal' paths if lookup time for specific strings was too slow in the database you chose.

When a request comes in, look it up in the database and send back a 302 response with the 'target' URL as the new location for the resource.


This is done using a rewrite engine that is built into common webservers like Apache or Nginx. These engines allow you to write rules that transform a url like the first one into something that would be better understood by your php pages. For example, you could create rules that would turn your first link above into:

http://newsnow.co.uk/index.php?catagory=A&id=471722742&referrer=-19721

This is transparent to the user and they will only ever see the link they first typed in. You can then use the variables being passed in to perform whatever actions you desire. In this case you might want to use the variables to perform some kind of database lookup to retrieve the actual destination that you are interested in. Then it's just a question of performing a php redirect to the link in question.

Check out the following link for a very quick intro to Apache's rewriting capabilities (called mod_rewrite): http://www.besthostratings.com/articles/mod-rewrite-intro.html

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜