1

In my web app, i'm using Amazon S3 bucket to hold images and i need my codeigniter host to show images from S3 bucket but with my host url.

For example:

mywebapp.com/products/image1.jpg will display content from mywebapp.s3.amazonaws.com/products/image1.jpg

I'm using Codeigniter and i'm not sure if i will handle this problem inside my codeigniter project or from other configurations.

xiankai
  • 2,773
  • 3
  • 25
  • 31
Yusuf Can Gürkan
  • 725
  • 2
  • 10
  • 20
  • Why not just use the S3 link directly whenever you need it? We'll at least need to know what exactly you want to achieve. – xiankai Oct 15 '13 at 13:31
  • I know its not needed but i just thought that this kind of url redirect will seem more professional and also i want my all urls have same origin for a better look. – Yusuf Can Gürkan Oct 15 '13 at 14:26
  • Then what you want is not url redirection, it is domain masking. http://en.wikipedia.org/wiki/Domain_Masking – xiankai Oct 16 '13 at 03:33

1 Answers1

0

First load the url helper in your constructor if you haven't done it already:

$this->load->helper('url');

then whenever you need the redirect you can simply call:

$s3_url = "https://mywebapp.s3.amazonaws.com/products/image1.jpg";

// you can omit the last two parameters for the default redirect
redirect($s3_url, 'location', 301);

I guess you want a service where you access the url and get the image, here's my solution

<?php if (!defined('BASEPATH')) die();
class Img extends CI_Controller {

    public function __construct ()
    {
        parent::__construct();

        $this->load->helper('url');

        // this is the db model where you store the image's urls
        $this->load->model('images_model', 'img_m');
    }

    // accessed as example.com/img/<image_id>
    // redirects to the appropiate s3 URL
    public function index()
    {
        // get the second segment (returns false if not set)
        $image_id = $this->uri->segment(2);

        // if there was no image in the url set:
        if ($image_id === false)
        {
            // load an image index view
            $this->load->view('image_index_v');
            exit;
        }

        $url = $this->img_m->get_url($image_id);

        // get_url() should return something like this:
        $url = "https://mywebapp.s3.amazonaws.com/products/image1.jpg";

        // then you simply call:
        redirect($url, 'location', 301);
    }
}
Josue Alexander Ibarra
  • 8,269
  • 3
  • 30
  • 37