Announcement

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

ahtoagah
Member
2019-01-25
23

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

 

#2 2019-06-03 17:29:07

pre
Member
2019-06-03
1

Re: Solved: how to generate friendlier urls

Hi,

this was very helpful for me! Thanks for sharing!

Just one issue I noticed: if you have pagination in your albums, the links to the pages are broken as they are not correctly rewritten by your rules. However I could easily add one more rule which will fix this. So for everyone else that has pagination issues with the above solution, just add the following rule:

Code:

# redirect incoming short category url with pagination back to its original long url:
RewriteCond %{REQUEST_URI} !^(.*)php(.*)$
RewriteCond %{REQUEST_URI} ^(.*)start-([0-9]+)$
RewriteRule ^/([a-zA-Z0-9_-]+)/start-([0-9]+)$ /index.php/category/$1/start-$2 [NC,NE,L]

Another small edge case that is not covered so far: if your album name contains the string "php" the rewrite will not work at all since this part will not match:

Code:

!^(.*)php(.*)$

But since this is a really unlikely edge case I did not change it :) (e.g. if you have an album called "josephphotos").

Thanks again for the effort!

Best regards
pre

Offline

 

#3 2019-06-03 22:05:32

EdwinKort
Translation Team
Rhoon, the Netherlands
1970-01-01
95

Re: Solved: how to generate friendlier urls

How I would have loved this to work, unfortunately, it doesn't for me :(

Parse error: syntax error, unexpected end of file in voorugezien/include/functions_url.inc.php on line 841

It's just a ?> there, so I believe I did something wrong with the changing of the script functions_url.inc.php

Should I have added the changed code to function make_index_url and function make_picture_url  or replace the old content of it with the new code?


My Gallery
Hosted with: Surpass Hosting since 2004

Offline

 

#4 2019-06-04 03:35:22

ahtoagah
Member
2019-01-25
23

Re: Solved: how to generate friendlier urls

To Pre:

Glad you were able to make it work. Good catch about the pagination issue! I didn't test it in my own site, since I make sure to put all my album photos on a single page. I will add your code to mine, anyway. It's possible I could decide to paginate in the future.

About the rare case of an album name "innocently" including the string "php" in its name, I imagine just changing "^(.*)php(.*)$" to "!^(.*).php(.*)$" would take care of the issue. But I also now wonder if the problematic line -- RewriteCond %{REQUEST_URI} !^(.*)php(.*)$ -- is even necessary, since the very first passthrough [PT] rule would filter any .php URIs and they would never even reach the last RewriteCond. Later on I'll try deleting it and see what happens.


To EdwinKort:

What I did really was just insert two lines near the end of the make_index_url function at the original line 151 (just above return $url; ):
    $url = str_replace("category/","",$url);
    $url = str_replace("picture.php/","",$url);

And I also inserted two more lines near the end of the function make_picture_url at the original line 264 (just above return $url; ):
    $url = str_replace("category/","",$url);
    $url = str_replace("index.php/","",$url);

These lines are insertions, not replacements. The rest of the code in the functions_url.inc.php file remained EXACTLY the same as the original.

I'm sorry my post wasn't clear. Unfortunately, the "Code" block in these forum posts do not permit adding colours to the lines, or I would have highlighted only the newly inserted lines of text. But I hope this clarifies what I did and that it can work for you. Otherwise, I'm not sure what to suggest.

Offline

 

#5 2019-06-09 03:46:41

ahtoagah
Member
2019-01-25
23

Re: Solved: how to generate friendlier urls

I tidied up the code to Rewrite incoming URIs. Two main changes:
-- I had forgotten to add or escape the period (full-stop) character in the string ".php" so now they are all "\.php". This should also take care of the rare problem when the string "php" is actually a part of the category name.
-- I added the code from user "pre" to handle categories (albums) with pagination

I did NOT verify that the Rewrite code or the changes to include/functions_url.inc.php in piwigo 2.9.5 or 2.10 (beta). I only modified 2.9.4. It would be useful if anyone here could verify these these code changes in other versions of piwigo.

Now the complete Rewrite code looks like this:

Code:

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

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

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

#  redirect incoming short picture uri (in the form "/961/album-5-bangkok") to its original long uri:
  RewriteCond %{REQUEST_URI} !^(.*)\.php(.*)$
  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 uri (without pagination) back to its original long uri:
  RewriteCond %{REQUEST_URI} !^(.*)\.php(.*)$
  RewriteRule ^/([a-zA-Z0-9_-]+)$ /index.php/category/$1 [NC,NE,L]
  
#  redirect incoming short category uri (with pagination) back to its original long uri (!! thanks to user "pre"):
  RewriteCond %{REQUEST_URI} !^(.*)\.php(.*)$
  RewriteCond %{REQUEST_URI} ^(.*)start-([0-9]+)$
  RewriteRule ^/([a-zA-Z0-9_-]+)/start-([0-9]+)$ /index.php/category/$1/start-$2 [NC,NE,L]

Let me know if I accidentally created other problems, or if there are other issues with the customisations.

Offline

 

#6 2019-12-04 12:07:02

Klug
Member
2019-12-04
1

Re: Solved: how to generate friendlier urls

Hello.

This works perfectly with the last version (I setup piwigo yesterday, so it's 2.10.1).
Thank you.

It definitively should be in the wiki/FAQ or integrated in piwigo.

Last edited by Klug (2019-12-04 12:16:47)

Offline

 

#7 2020-01-05 23:37:47

Sambob
Member
2019-01-23
5

Re: Solved: how to generate friendlier urls

Hi All, first post and just installed PIWIGO, looks great so far. (I come from a Joomla background, and as fo Galleries, I have used Coppermine and Gallery.

The .htaccess looks good, but my install is not reading it.

My PIWIGO install Red Dust Snow

The .htaccess file is in the root.

Where should the htaccess be (or am I missing something?)

Great work/effort!

Offline

 

#8 2020-01-06 08:00:12

ahtoagah
Member
2019-01-25
23

Re: Solved: how to generate friendlier urls

Hi,
Normally, I believe that an .htaccess should go in piwigo root... On my machine that's /var/www/piwigo/. You may need to set or modify the "AllowOverride all" directive in your piwigo conf file or perhaps in your main apache conf.

The main apache tutorial on .htaccess is quite good: https://httpd.apache.org/docs/current/h … ccess.html
Note that they discourage using .htaccess files at all, mainly for performance reasons. That's one of the reasons I put those rewrite directives inside my piwigo conf.

Hope this helps. Good luck!

Offline

 

#9 2020-01-06 08:42:58

Sambob
Member
2019-01-23
5

Re: Solved: how to generate friendlier urls

Hi Ahtoagah,

That makes sense, thanks for the heads-up on the config file.

I will give that a go.

Simon

Offline

 

#10 2020-01-06 14:16:06

ahtoagah
Member
2019-01-25
23

Re: Solved: how to generate friendlier urls

Just to clarify, I don't mean the piwigo internal config file: /.../piwigo/include/config.default.inc.php . That has nothing to do with .htaccess. Not sure if I was unclear about that.

I am talking about the apache "conf" files, usually found in /etc/apache2/ and /etc/apache2/conf-available/. Those are normally where the website "conf" files are stored.

Just so there's no confusion... 8-)

Offline

 

#11 2020-01-08 00:12:32

Sambob
Member
2019-01-23
5

Re: Solved: how to generate friendlier urls

Hi Ahtoagah,

Yeah, I think I knew what you were referring to. But still no luck.

Firstly, I have tested htaccess file with a basic redirect and it definitely works. It just doesnt work with the directives listed above for PIWIGO.

You mentioned in your first reply "I put those rewrite directives inside my piwigo conf" . Exactly which config file are you refer to? And where within that file do the directives go?

Thanks again, in advance. Really keen to get the SEF Urls sorted.

Simon Bayliss

Offline

 

#12 2020-01-08 08:49:49

ahtoagah
Member
2019-01-25
23

Re: Solved: how to generate friendlier urls

Here is the virtual server for my piwigo. You can see I just embedded the rewrites after the ServerName and before my SSL stuff. My DocumentRoot directives come at the end.:

<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName travelrainbow.servehttp.com

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

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

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

#  redirect incoming short picture uri (in the form "/961/album-5-bangkok") to its original long uri:
  RewriteCond %{REQUEST_URI} !^(.*)\.php(.*)$
  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 uri (e.g. "/album-5-bangkok", no pagination) back to its original long uri:
  RewriteCond %{REQUEST_URI} !^(.*)\.php(.*)$
  RewriteRule ^/([a-zA-Z0-9_-]+)$ /index.php/category/$1 [NC,NE,L]
 
#  redirect incoming short category uri (with pagination) back to its original long uri (!! thanks to user "pre"):
  RewriteCond %{REQUEST_URI} !^(.*)\.php(.*)$
  RewriteCond %{REQUEST_URI} ^(.*)start-([0-9]+)$
  RewriteRule ^/([a-zA-Z0-9_-]+)/start-([0-9]+)$ /index.php/category/$1/start-$2 [NC,NE,L]

IncludeOptional /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/ [REMOVED]
SSLCertificateKeyFile /etc/letsencrypt/ [REMOVED]

DocumentRoot /var/www/piwigo
<Directory /var/www/piwigo/>
    Options +MultiViews
    Allow from all
    Satisfy any
</Directory>

</VirtualHost>
</IfModule>

Offline

 

#13 2020-01-20 21:39:34

ahtoagah
Member
2019-01-25
23

Re: Solved: how to generate friendlier urls

I discovered that the "search" function didn't work with these hacks. I solved it by inserting into the Rewrite statements the statement below in bold face. All other changes presented earlier are unchanged:

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

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

# passthrough empty (Home) uri:
    RewriteRule ^$ - [PT,L]

# redirect search uri (in the form to its original long uri:
    RewriteCond %{REQUEST_URI} ^/search/(.*)$
    RewriteRule ^/search/([a-zA-Z0-9_-]+)$ /index.php/search/$1 [NC,NE,L]


# redirect incoming short picture uri (in the form "/961/album-5-bangkok") to its original long uri:
    RewriteCond %{REQUEST_URI} !^(.*)\.php(.*)$
    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 uri (e.g. "/album-5-bangkok", no pagination) back to its original long uri:
    RewriteCond %{REQUEST_URI} !^(.*)\.php(.*)$
    RewriteRule ^/([a-zA-Z0-9_-]+)$ /index.php/category/$1 [NC,NE,L]
 
# redirect incoming short category uri (with pagination) back to its original long uri (!! thanks to user "pre"):
    RewriteCond %{REQUEST_URI} !^(.*)\.php(.*)$
    RewriteCond %{REQUEST_URI} ^(.*)start-([0-9]+)$
    RewriteRule ^/([a-zA-Z0-9_-]+)/start-([0-9]+)$ /index.php/category/$1/start-$2 [NC,NE,L]

Offline

 

#14 2020-04-16 17:06:42

tkrn
Member
2020-04-16
6

Re: Solved: how to generate friendlier urls

I'm having an issue with the Piwigo rewrites. I don't know if this is because the installation is in a subdirectory /g and not the root.

I keep on receiving this error:

Not Found
The requested URL /g/events was not found on this server.

I believe it's because it's located in a subdirectory, but I'm not exactly sure on how to go about solving it.

Offline

 

#15 2020-05-14 14:35:16

ahtoagah
Member
2019-01-25
23

Re: Solved: how to generate friendlier urls

Sorry I didn't reply earlier. I was stuck in Tunisia for 1-1/2 months and only last week managed to get a repatriation flight home. If you have already managed to solve your problem in the meantime, please post your solution here, for others to see.

If you are still looking for an answer, here are my thoughts...

Yes, I can see that a REQUEST_URI for piwigo in a subdirectory would present a problem for many of my rewrites. A few suggestions:
-- you might try using an Alias directive in your virtual server, so your subdirectory doesn't appear at all in the URI.
-- perhaps a RewriteBase directive might fix the problem for all rewrites? I've never used it, but the documentation seems to indicate it might work.
-- "hard code" the subdirectory into relevant RewriteConds or RewriteRules, e.g. RewriteRule ^/g/([a-zA-Z0-9_-]+)$ /g/index.php/category/$1 [NC,NE,L]
-- instead of inserting the actual subdirectory, you could try inserting a catch-all string such as /([0-9]+)/([a-zA-Z0-9_-]+) , but that would probably require adjusting quite a few other things in the entire Rewrite section, including the back-references.

These are all documented in Apache directives

That's all I can think of for now. Good luck!

Offline

 

Board footer

Powered by FluxBB

github twitter newsletter Donate Piwigo.org © 2002-2024 · Contact