开发者

Mask all outgoing links

I have a software download 开发者_运维知识库site where all the software is hosted on Amazon S3. I am using WordPress for my site and I don't want my visitors to know that I am hosting all of the software on Amazon S3. I want to rewrite all Amazon S3 URIs to my site url and whenever visitors click those URIs they should be redirected to Amazon S3...

I tried the Pretty Links Lite plugin to hide the Amazon S3 links, but that plugin is extremely slow and lacks support.

Does anyone have some advice or a better recommendation of how to accomplish this?


Unless you want to stream your downloads via your server (which would ruin the point of hosting on Amazon S3), you won't be able to mask the fact your downloads are on Amazon S3.


Joining the game a little late but I ran into the same issue with Pretty Links Lite. The more Pretty Links you add the more it slows down your site, even with aggressive caching.

My solution was to make a custom post type called redirect and use some custom fields (though I use the Advanced Custom Fields plugin for a more elegant backend experience). Then simply add a quick function that hooks into template_redirect that checks for your post type.

The only drawback is that you need to assign a slug to your CPT, but you can easily customize that in the register function.

Here is my code:

function register_redirect_cpt {
  register_post_type('redirect', array(
    'label' => 'redirects',
    'labels' => array(
      'name' => 'Redirects',
      'singular_name' => 'Redirect',
      'add_new' => 'Add Redirect',
      'add_new_item' => 'Add New Redirect',
      'edit_item' => 'Edit Redirect',
      'new_item' => 'New Redirect',
      'view_item' => 'View Redirect',
      'search_items' => 'Search Redirects',
      'not_found' => 'No Redirects found',
      'not_found_in_trash' => 'No Redirects found in Trash'
    ),
    'description' => 'Pretty Redirects',
    'public' => true,
    'menu_position' => 5,
    'supports' => array(
      'title',
      'author',
      'custom-fields' // This is important!!!
    ),
    'exclude_from_search' => true,
    'has_archive' => false,
    'query_var' => true,
    'rewrite' => array(
      'slug' => 'redirect',
      'with_front' => false
    )
  ));
}
add_action('init', 'register_redirect_cpt') ;

As I said, you can use custom fields or the ACF plugin to setup a few metaboxes–1 for the public link and the other for the true destination. I'll assume you use vanilla custom fields. Then plug this into your functions.php or theme functions file:

function redirect_for_cpt() {
  if (!is_singular('redirect')) // If it's not a redirect then don't redirect
    return;

  global $wp_query;

  $redirect = isset($wp_query->post->ID) ? get_post_meta($wp_query->post->ID, '_true_destination', true) : home_url(); // If you forget to set a redirect then send visitors to the home page; at least we avoid 404s this way!

  wp_redirect(esc_url_raw($redirect), 302);
  exit;

}
add_action('template_redirect', 'redirect_for_cpt');
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜