Announcement

#1 2019-01-25 12:22:28

ahtoagah
Member
2019-01-25
1

Solved: how to generate friendlier urls

Hello/Hi/Greetings,

Here is a crude solution to changing the "old-fashioned" urls PWG uses for albums and images (i.e., "categories" and "pictures"). I didn't like having "index.php", "picture.php", and "category" in my urls, so that motivated me to try to improve them.

I did succeed to create urls which look like these:
    https://travelrainbow.servehttp.com/living-in-france {for albums}
    https://travelrainbow.servehttp.com/5258/on-the-streets {for photos}

My strategy was:
-- to hack PWG to generate short urls (can be seen by hovering over links).
-- to create Apache rewrites to redirect those incoming short urls back to PWG in their original format.

Steps:
1.    I used LocalFiles Editor plugin to add the following settings to local/config/config.inc.php :
        $conf['question_mark_in_urls'] = false;
        $conf['category_url_style'] = 'id-name';
    I also used the Permalink Generator plugin to make the urls a bit more friendly.

After those changes, PWG generated urls like these, better than the default, but still ugly:
    https://.../index.php/category/living-in-france
    https:/.../picture.php/5258/category/on-the-streets

2.    I then began to look for modifications to code files. I needed to hack only one file: include/functions_url.inc.php, with two changes in it:
        -- I modified function make_index_url to strip "index.php" and "category" from all album urls.
            {New short album format https://.../living-in-france}
        -- I modified function make_picture_url to strip "picture.php" and "category" from all picture urls.
            {New short picture format: https://.../5258/on-the-streets}

Here are the two functions I modified (the changes appear just above "return $url;") :

Code:

function make_index_url($params = array())
{
  global $conf;
  $url = get_root_url().'index';
  if ($conf['php_extension_in_urls'])
  {
    $url .= '.php';
  }
  if ($conf['question_mark_in_urls'])
  {
    $url .= '?';
  }

  $url_before_params = $url;

  $url.= make_section_in_url($params);
  $url = add_well_known_params_in_url($url, $params);

  if ($url == $url_before_params)
  {
    $url = get_absolute_root_url( url_is_remote($url) );
  }
/*
     The following lines create a new "short" category url
     to substitute for the original long url used by templates.
*/
$url = str_replace("category/","",$url);
$url = str_replace("index.php/","",$url);

  return $url;
}
  ...  ...  ...  ...  ...  ...

function make_picture_url($params)
{
  global $conf;

  $url = get_root_url().'picture';
  if ($conf['php_extension_in_urls'])
  {
    $url .= '.php';
  }
  if ($conf['question_mark_in_urls'])
  {
    $url .= '?';
  }
  $url.= '/';
  switch ( $conf['picture_url_style'] )
  {
    case 'id-file':
      $url .= $params['image_id'];
      if ( isset($params['image_file']) )
      {
        $url .= '-'.str2url(get_filename_wo_extension($params['image_file']));
      }
      break;
    case 'file':
      if ( isset($params['image_file']) )
      {
        $fname_wo_ext = get_filename_wo_extension($params['image_file']);
        if ( ord($fname_wo_ext)>ord('9') or !preg_match('/^\d+(-|$)/', $fname_wo_ext) )
        {
          $url .= $fname_wo_ext;
          break;
        }
      }
    default:
      $url .= $params['image_id'];
  }
  if ( !isset($params['category'] ) )
  {// make urls shorter ...
    unset( $params['flat'] );
  }
  $url .= make_section_in_url($params);
  $url = add_well_known_params_in_url($url, $params);

/*
     The following lines create a new "short" picture url
     to substitute for the original long url used in templates.
*/
$url = str_replace("category/","",$url);
$url = str_replace("picture.php/","",$url);

  return $url;
}

3.    Lastly, I made RewriteRule directives in my virtual server to receive incoming short urls, modify them back to PWG's long format, and then send them on to PWG for processing. I also needed to allow the other urls (identifification.php, admin.php, etc.) to pass through unchanged. (These RewriteRules should also work in .htaccess):

Code:

  RewriteEngine On
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d

#  passthrough any url with ".php" in it:
  RewriteRule ^(.+).php(.*)$ - [PT]

#  passthrough empty (Home) url:
  RewriteRule ^$ - [PT]

#  redirect incoming short picture url (in the form "/961/album-5-bangkok") to its original long url:
  RewriteCond %{REQUEST_URI} ^/([0-9]+)/([a-zA-Z0-9_-]+)$
  RewriteRule ^/([0-9]+)\/([a-zA-Z0-9_-]+)$ /picture.php/$1/category/$2 [NC,NE,L]

#  redirect incoming short category url (in the form "/album-5-bangkok") back to its original long url:
  RewriteCond %{REQUEST_URI} !^(.*)php(.*)$
  RewriteRule ^/([a-zA-Z0-9_-]+)$ /index.php/category/$1 [NC,NE,L]

One extra benefit is that RV Sitemap now produces "clean" short urls. (I suppose other sitemap generators would also use the shorter urls.)

A caveat: I only use a minimal web page interface and PWG features (no tags, no comments, no favorites, etc.)... I only use albums, photos and descriptions. I am not sure how these hacks would affect those other features.

In fact, I feel it would be optimal modify the database to add a "short url" field to the images and categories tables. But that is not a problem I could quickly solve. I also did not modify how PWG receives and parses urls. That's the reason I used RewriteRules.

If anyone here can find errors or inefficiencies in my efforts, I'd be interested to hear about them. I'm just an amateur and am certain there are better ways to do things. I've probably missed some other solutions in this forum but here's a couple hacks which worked for me...

Mike

Piwigo version: 2.9.4, elegant theme
Ubuntu 16.04
Apache version: 2.4
PHP version: 7.2
MySQL server version: 5.7
Piwigo URL: https://travelrainbow.servehttp.com

Last edited by ahtoagah (2019-01-25 12:28:05)

Offline

 

Board footer

Powered by FluxBB

github twitter facebook google+ newsletter Donate Piwigo.org © 2002-2019 · Contact