Created
February 15, 2023 15:34
-
-
Save jesusbagpuss/2e9172a2ee94d4dcfc5f23a08486f3fe to your computer and use it in GitHub Desktop.
Add to EPRINTS_ROOT/archives/ARCHIVE_ID/cfg/cfg.d/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Map specific URLs to be permanently redirected | |
use EPrints::Const; # for trigger return values | |
# define specific URLs that have been moved. | |
# If all the new URLs are to the same base URL, you could have e.g 1234 => 5678 in the | |
# hash, and prepend a static 'https://new.repo.com/' in the 'Location' line below | |
$c->{z_url_rewrite_map} = { | |
'1234' => 'https://abc.de/1/', | |
'2345' => 'https://abc.de/9/', | |
}; | |
$c->add_trigger( EP_TRIGGER_URL_REWRITE, sub { | |
my( %o ) = @_; | |
# Available hash keys in %o | |
# request, lang (en), args ("" or "?foo=bar"), urlpath ("" or "/subdir"), cgipath ("/cgi" or "/subdir/cgi") | |
# uri (/foo/bar), secure ( boolean ), return_code (set to trigger a return - stop stack of triggers), repository | |
my $redirect_map = $o{repository}->get_conf( "z_url_rewrite_map" ); | |
return if !defined $redirect_map; | |
# Optimisation over the 404 handler regex... | |
# If you have a long-standing repository, there may be EPrint URLs out in the wild that refer to repo.com/1234 (without trailing slash). | |
# Currently, EPrints will redirect this to repo.com/1234/ - and then m#^/(\d+)/# (from 404 handler) would capture it. | |
# This could result in multiple redirects ( /1234 --> /1234/ --> other server ) which isn't best practice for SEO. | |
# | |
# The first regex below will match /1234 or /1234/. | |
# If your repo is _really_ old, you might also want to capture repo.com/archive/1234 or repo.com/archive/00001234... | |
# ...I guess looking in the Apache logs will indicate if this is necessary! | |
if( defined $o{uri} && ( $o{uri} =~ m#^/(\d+)(?>/|$)# || $o{uri} =~ m#^/id/eprint/(\d+)/# ) ) | |
{ | |
# We've got what looks like an EPrintID... | |
# Is there a redirect map for it? | |
if( defined $redirect_map->{$1} ){ | |
EPrints::Apache::AnApache::send_status_line( $o{request}, 301, "Moved Permanently" ); | |
EPrints::Apache::AnApache::header_out( $o{request}, "Location", "$redirect_map->{$1}" ); | |
EPrints::Apache::AnApache::send_http_header( $o{request} ); | |
${$o{return_code}} = EPrints::Const::DONE; | |
return EP_TRIGGER_DONE; | |
} | |
# an EPrintID, but not redirected. | |
} | |
# not an EPrintID URL - just return (could explicitly set a trigger return value, but not necessary) | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment