Clean URLs for tags - a simple hack

I haven't seen this addressed previously. Please let me know if it has.

I love the addition of tagging (and someday maybe a normalized database for them :) ), but I hate the URLs. So, I have a simple hack to make this:
`http://elbedesign.com/zenphoto/index.php?p=search&searchfields=4&words=sometag`
look like this:
`http://elbedesign.com/zenphoto/tags/sometag`

(this hack obviously requires that mod_rewrite is enabled. Well, I check to ensure that it is, so you can implement this hack on your site, but it won't do anything unless your server can support it (and zenphoto is configured to enable mod_rewrite)

I added these lines to the .htaccess:
` #### Rewrite rule for tags clean URL

#### SEE ALSO: hack in template-functions.php - FUNCTION "getSearchURL" -- insert 4 lines starting at line 1967 ####

RewriteRule ^tags/(.*) index.php?p=search&searchfields=4&words=$1 [L,QSA]

`
(You have to add this rewrite rule ABOVE the "catch-all" rule)

and I added 4 new lines at the start of the `getSearchURL` function in `template-functions.php` (inserted lines start at line 1967 in release 1.1.3)
`function getSearchURL($words, $dates, $fields=0) {

if ( !empty($words) && empty($dates) && $fields==SEARCH_TAGS && getOption('mod_rewrite') ) { // elbe added (hack for clean tags URL)

// if request is for a "tags" url AND mod_rewrite is enabled, give them a clean /tags/ URL // elbe added (hack for clean tags URL)

return WEBPATH."/tags/".$words; // elbe added (hack for clean tags URL)

} // elbe added (hack for clean tags URL)

$url = WEBPATH."/index.php?p=search";

if($fields != 0) { $url .= "&searchfields=$fields"; }

if (!empty($words)) { $url .= "&words=$words"; }

if (!empty($dates)) { $url .= "&date=$dates"; }

return $url;

}

`

I can make the hacked versions of these files available for download if necessary, but it is a pretty easy edit (and I don't really trust myself to update the hack with every point release).

All standard disclaimers apply -- make backups, hack at your own risk, I cannot be held responsible for any unforseen breakage. but it works on my test site...

Comments

  • acrylian Administrator, Developer
    Good idea, we simply forgot rules for the tag search. I think we will implement that.
  • acrylian Administrator, Developer
    I took a deeper look to your hack. I reworked it so that it fits our general search paths which follows the zp custom page path like `zenphoto/page/search`. Now your tags path would be `zenphoto/page/search/tags`just to keep in line with the existing. Maybe we skip the `page` someday, I guess we kept it to avoid conflicts with album titles.

    Additionally I have added mod_rewrite rules for result pagination (that you maybe forgot) and for the archive date search too. Both needed a little more tweaking that just getSearchURL.

    I already sent it to my fellow developers for testing, so probably that will be in one of the next svns.
  • acrylian, good point about path consistency. I was actually inspired by this forum comment, which I forgot to reference in my initial post. So I just aped that URL structure.
  • After updating to the most recent nightly build, the archive does not work anymore. The cause is related to "Changeset 1030". A click on the date link leads to an 404 page. Before I did the update, the url looks like this:
    http://www.domain.com/gallery/index.php?p=search&date=2008-01
    after update:
    http://www.domain.com/gallery/page/search/archive/2008-01

    Regards,
    Steffen
  • acrylian Administrator, Developer
    Right, if mod_rewrite is enabled the path should look like that. That's what I added. Probably you need to update you htaccess file with the new rewrite rules. The file should be included in the nightly build (probably invisible on your system).
  • I also copied your .htaccess file from the (latest) nightly build. Since my gallery is not installed in /zenphoto, I had to change the RewriteBase like this:

    RewriteBase /zenphoto --> to RewriteBase /gallery

    The new "clean URLs" does not work for me. I only get 404 pages. If I update just the template-functions.php and keep the older .htacces, I get the following error message:

    Zenphoto Error: the requested object was not found. Please go back and try again.
  • acrylian Administrator, Developer
    Strange, it works for me. Only updating template functions.php without the new htaccess can't work.
    Did you run setup.php after upgrading (that should set the rewritebase automatically)? Does archive work with disabled mod_rewrite?
  • What I found out is, that the rewrite rule works for tags, but not for the archive. You can have a look on my temporary site: www.synnatschke.com/gallery

    If you click on the tag "sandstone", you will find all the related pictures. But if you switch to the archive: http://www.synnatschke.com/gallery/?p=archive and click on "January (6)", you will get an error message.
  • acrylian Administrator, Developer
    Could you please post your htaccess file? Seems that there is something missing since the normal path `http://www.synnatschke.com/gallery/index.php?p=search&date=2008-01` works.
  • Sure! Here it comes...

    # htaccess file for zenphoto
    # NOTE: Change the RewriteBase below to the absolute path to your zenphoto directory.

    <IfModule mod_rewrite.c>
    RewriteEngine On

    #### !!! Change this to the web path (eg: http://www.yourwebsite.com/photos --> /photos) !!!
    RewriteBase /gallery
    ####

    RewriteRule ^admin/?$ zp-core/admin.php [R,L]

    RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
    RewriteCond %{REQUEST_FILENAME} -d [NC]
    RewriteRule ^.*$ - [R,L]

    RewriteRule index\.php$ index.php [L,QSA]
    RewriteRule ^page/([0-9]+)/?$ index.php?page=$1 [L,QSA]
    RewriteRule ^page/([A-Za-z0-9\-_]+)/?$ index.php?p=$1 [L,QSA]
    RewriteRule ^(.*)/page/([0-9]+)/?$ index.php?album=$1&page=$2 [L,QSA]

    #### Rewrite rule addtion for seach
    RewriteRule ^page/([A-Za-z0-9\-_]+)/([A-Za-z0-9\-_]+)/?$ index.php?p=$1&words=$2 [L,QSA]
    RewriteRule ^page/([A-Za-z0-9\-_]+)/([A-Za-z0-9\-_]+)/([0-9]+)/?$ index.php?p=$1&words=$2&page=$3 [L,QSA]

    #### Rewrite rule addtion for archive-/date-search (needed to be separarted because of the same structur)
    RewriteRule ^page/([A-Za-z0-9\-_]+)/tags/(.*)/([0-9]+)/?$ index.php?p=$1&searchfields=4&words=$2&page=$3 [L,QSA]
    RewriteRule ^page/([A-Za-z0-9\-_]+)/tags/(.*)/?$ index.php?p=$1&searchfields=4&words=$2 [L,QSA]
    RewriteRule ^page/([A-Za-z0-9\-_]+)/archive/([A-Za-z0-9\-_]+)/?$ index.php?p=$1&date=$2 [L,QSA]
    RewriteRule ^page/([A-Za-z0-9\-_]+)/archive/([A-Za-z0-9\-_]+)/([0-9]+)/?$ index.php?p=$1&date=$2&page=$3 [L,QSA]

    RewriteRule ^(.*)/image/(thumb|[0-9]{1,4})/([^/\\]+)$ zp-core/i.php?a=$1&i=$3&s=$2 [L,QSA]
    RewriteRule ^(.*)/image/([^/\\]+)$ zp-core/i.php?a=$1&i=$2 [L,QSA]
    ####

    # Catch-all - everything else gets handled in PHP for compatibility.
    RewriteRule ^(.*)/?$ index.php?album=$1 [L,QSA]
    </IfModule>
  • acrylian:

    What does not work is the link generated when you click on a date in the archive. What I get for a url is:

    `http://testalbum.sbillard.org/page/search/archive/2003-07`

    This gives the `Zenphoto Error: the requested object was not found. Please go back and try again. ` error message.

    This is with the nightly build .htaccess and having run setup. (All other mod_rewrite stuff that I have tried is workign.)
  • acrylian Administrator, Developer
    Steffen: We or more exactly sbillard figured it out. You need to change the archives rules a little to
    `RewriteRule ^page/([A-Za-z0-9-_]+)/archive/(.*)/?$ index.php?p=$1&date=$2 [L,QSA]

    RewriteRule ^page/([A-Za-z0-9-_]+)/archive/(.*)/([0-9]+)/?$ index.php?p=$1&date=$2&page=$3 [L,QSA]`

    It's in the next svn, too.
  • Thank you for your support!
  • The bug fix (htaccess) you released last night works only on the first page.

    In my archive, I have 24 images for 1 month. 18 of them are shown on the first page, the other ones should be visible on the second page, but they dont. With mod_rewrite "on", I get the error "Sorry, no image matches. Try refining your search.". If I don't use mod_rewrite, I also get the second page with images. For verification, please see: http://www.synnatschke.com/gallery/page/search/archive/2008-01

    Just another strange thing: I set the option "Image per page" to 15, which does not work properly, because I get 18 images on 1 page (see link above).
  • acrylian Administrator, Developer
    Could you please stop reporting bugs...:-)) You are right, there are two lines reversed in the htaccess:
    ' RewriteRule ^page/([A-Za-z0-9\-_]+)/archive/(.*)/?$ index.php?p=$1&date=$2 [L,QSA]
    RewriteRule ^page/([A-Za-z0-9\-_]+)/archive/(.*)/([0-9]+)/?$ index.php?p=$1&date=$2&page=$3 [L,QSA] `

    That should be:

    `RewriteRule ^page/([A-Za-z0-9\-_]+)/archive/(.*)/([0-9]+)/?$ index.php?p=$1&date=$2&page=$3 [L,QSA]
    RewriteRule ^page/([A-Za-z0-9\-_]+)/archive/(.*)/?$ index.php?p=$1&date=$2 [L,QSA]
    `
    Sorry.

    Regarding the images per page behaviour please look at this thread: http://www.zenphoto.org/support/topic.php?id=1911&replies=5
Sign In or Register to comment.