1

I'll start with an example.

An image with an embedded watermark URL example.com/1203020202, in the lower right-hand corner in small text will serve as a redirect to the original URL. So when a user visits example.com/1203020202, they will be redirected to the original URL if it still exists i.e. example.com/something/this-is-the-image-page. This is so when images are sent via email or shared, found on google images, it serves as a URL shortener and 301 redirects the user to the original source.

EE 2.8 has the new template routing. I'm wondering if it would be better to write an module/extension hook that would do the routing before template parsing. I speculate that this will require more work, but not sure if it's worth it in the end. Does the entry exist, is the status open or closed? etc, etc.

Another option is to use the EE 2.8 routing with a template that would find the entry_id and perform the redirect if {segment:id} is found.

One thing that scares me about the EE2.8 routing is that seems limited by priority, and correct me if I'm wrong, there is no way to set priority with routing. Edit, as of EE2.9 you can set template routing priorities

I'm thinking of speed overall, and if it can be done before the template parser begins, it's faster, less code, that would be cool.

Any ideas?

5
  • Hi! What is the number on segment_1? Why not create a segment of just one letter? For example, example.com/i/1203020202?
    – Sobral
    Jul 26, 2014 at 13:49
  • The number is just an id reference that ties to an entry, it could be even be the entry_id itself or a randomly generated unique value that is stored in a text field. I would like to keep the URL watermark as short as possible instead of including a letter and more forwarded slashes.
    – Bransin
    Jul 26, 2014 at 23:24
  • Well, you gonna have some problems. First, if you use segment_1, you gonna have to search all pages every time somebody try to open something different from the homepage, before look for an image. Then, you can't use a route starting with a variable segment, because this can cause conflicts. Let's say, somebody tries to save a page as a number! If you use one single static segment, you can make everything easier. Even better, you can set two different domains on the same directory of the server, one of them being the short version: exp.com/i/1203020202.
    – Sobral
    Jul 27, 2014 at 0:40
  • Thanks for your thoughts Rob, and it gives me ideas but also raises questions. I'm wondering what extension hook would be used before the template parser begins and after the segment_x routing? What if the extension hook method (before template parsing and after segment_x routing) simply performed a return; if the query id was not found within a specific channel id and specific field? With using a return; and the correct hook, the native template routing would continue.
    – Bransin
    Jul 27, 2014 at 4:30
  • Just noticed in EE2.9 you can actually rearrange the order of template routes. Now I'm curious about when methods fire internally. If I can bypass the template parser for speed, that would be ideal.
    – Bransin
    Jul 27, 2014 at 8:13

1 Answer 1

4

I can think of two ways to do this. One is to use Resource Router. Your config would look something like this:

$config['resource_router'] = array(
  ':num' => function($router, $wildcard) {
    $query = ee()->db->select('url_title')
      ->where('entry_id', $wildcard->value)
      ->where('channel_id', 1) // use the right channel id here
      ->get('channel_titles');

    $url_title = $query->row('url_title');

    $query->free_result();

    if ($url_title)
    {
      $router->redirect('your/template/'.$url_title);
    }
    else
    {
      $router->set404();
    }
  },
);

The other is to do the same basic logic in an extension using the core_template_route hook:

function core_template_route($uri_string) {
  $query = ee()->db->select('url_title')
    ->where('entry_id', ee()->uri->segment(1))
    ->where('channel_id', 1) // use the right channel id here
    ->get('channel_titles');

  $url_title = $query->row('url_title');

  $query->free_result();

  if ($url_title)
  {
    ee()->functions->redirect(ee()->functions->create_url('your/template/'.$url_title));
  }
);
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.