Looking for more efficient way to serve numerous link redirects?

最后都变了- 提交于 2019-12-13 14:28:17

问题


I'm wondering if there's a more efficient way to serve a large number of link redirects. For background: my site receives tens of thousands of users a day, and we "deep link" to a large number of individual product pages on affiliate websites.

To "cloak" the affiliate links and keep them all in one place, I'm currently serving all our affiliate links from a single PHP file, e.g. a user clicks on mysite.com/go.php?id=1 and is taken to the page on the merchant's site, appended with our affiliate cookie, where you can buy the product. The code I'm using is as follows:

<?php

$path = array(
‘1′ =>  ‘http://affiliatelink1.com’,
‘2′ =>  ‘http://affiliatelink2.com’,
‘3′ =>  ‘http://affiliatelink3.com’,

);

if (array_key_exists($_GET['id'], $path))
header(‘Location: ‘ .$path[$_GET['id']]);
?>

The problem I'm having is that we link to lots of unique products every day and the php file now contains 11K+ links and is growing daily. I've already noticed it takes ages to simply download and edit the file via FTP, as it is nearly 2MB in size, and the links don't work on our site while the file is being uploaded. I also don't know if it's good for the server to serve that many links through a single php file - I haven't noticed any slowdowns yet, but can certainly see that happening.

So I'm looking for another option. I was thinking of simply starting a new .php file (e.g. go2.php) to house more links, since go.php is so large, but that seems inefficient. Should I be using a database for this instead? I'm running Wordpress too so I'm concerned about using mySQL too much, and simply doing it in PHP seems faster, but again, I'm not sure.

My other option is to find a way to dynamically create these affiliate links, i.e. create another PHP file that will take a product's URL and append our affiliate code to it, eliminating the need for me to manually update a php file with all these links, however I'm not sure about the impact on the server if we're serving nearly 100K clicks a day through something like this.

Any thoughts? Is the method I'm using spelling certain death for our server, or should I keep things as is for performance? Would doing this with a database or dynamically put more load on the server than the simple php file I'm using now? Any help/advice would be greatly appreciated!


回答1:


What I would do is the following:

Change the URL format to have the product name in it for SEO purposes, such as something like "my_new_product/1"

Then use mod_rewrite to map that url to a page with a query string such as:

Rewriterule ^([a-zA-Z0-9_-]*)/([0-9]*)$ index.php?id=$2 [L]

Then create a database table containing the following fields:

id (autonumber, unique id) url (the url to redirect to) description (the text to make the url on your site)

Then, you can build a simple CRUD thing to keep those up to date easily and let your pages serve up the list of links from the DB.



来源:https://stackoverflow.com/questions/4307155/looking-for-more-efficient-way-to-serve-numerous-link-redirects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!