Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example rewrite trigger to map old path to new one
# This is an example of how to use the URL Rewrite trigger.
# In this case, specifically an archive that was initially configured with
# a base path (url) of '/usr/share/eprints3/data' rather than '' (no default path).
#
# This file should be saved to:
# <eprints_root>/archives/<archiveid>/cfg/cfg.d/z_rewrite_old_urls.pl
#
# This trigger will rewrite any requests that match:
# <host>/usr/share/eprints3/data/<something>
# to:
# <host>/<something>
# with a permanent redirect (301 response).
#
# For info, the hash keys in %o (passed into the Trigger) are:
# request # Apache2::RequestRec
# lang # e.g. 'en'
# args # "" or "?foo=bar"
# urlpath # "" or "/subdir"
# cgipath # /cgi or /subdir/cgi
# uri # /foo/bar - incoming request
# secure # boolean - true if request was https
# return_code # set this to trigger an http response of that code
# For more info see: https://wiki.eprints.org/w/Anatomy_of_a_request#EP_TRIGGER_URL_REWRITE
# or EPrints::Apache::Rewrite
use strict;
use EPrints::Const;
$c->add_trigger( EP_TRIGGER_URL_REWRITE, sub {
my( %o ) = @_;
my $old_path = "usr/share/eprints3/data";
if( my ($path) = $o{uri} =~ /^\/$old_path\/(.*?)$/ )
{
EPrints::Apache::AnApache::send_status_line( $o{request}, 301, "Moved Permanently" );
EPrints::Apache::AnApache::header_out( $o{request}, "Location", "/$path".$o{args} );
EPrints::Apache::AnApache::send_http_header( $o{request} );
${$o{return_code}} = EPrints::Const::DONE;
return EP_TRIGGER_DONE;
}
# if we're not doing anything, don't return anything, the request will continue as normal.
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment