From d61c6bbf52c93d3ab7bc5e87844dce6696c49e03 Mon Sep 17 00:00:00 2001 From: Philipp Neuhaus Date: Thu, 3 Mar 2022 14:44:30 +0100 Subject: [PATCH] Added an option to redirect links like "/redcap_latest/" as well. --- README.md | 7 +++++++ redcap_redirect.php | 20 +++++++++++++++----- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index c1da21e..49f2784 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,8 @@ If your apache server receives a request for a url that isn't on disk AND the ur The rewrite rule finds that /redcap_v8.1.0/index.php is NOT a valid file but has a 'redcap_vx.y.z' in it so it passes it to the redcap_redirect.php script which queries the database and determines that your redcap version is actually 9.2.5. So, it redirects the user to: `https://redcap.stanford.edu/redcap_v9.2.5/index.php?pid=10601` +- If uncommented, URLs like `https://redcap.stanford.edu/redcap_latest/index.php?pid=10601` will also get redirected to the redcap_redirect.php script + - If a url does NOT contain redcap_vx.y.z the rewrite rule will not take effect and the user will get the default Apache 404 error. - If a url redcap version is updated, but the file with the new version does not exist, the user will get a nice custom 404 error message: @@ -33,6 +35,11 @@ The rewrite rule finds that /redcap_v8.1.0/index.php is NOT a valid file but has RewriteCond %{REQUEST_URI} "^.*\/redcap_v(\d+\.\d+\.\d+)\/.*$" # Redirect to this script to handle the version substitution RewriteRule "^(.+)$" "/redcap_redirect.php" [PT,L,NS] + + #Optional: Uncomment to also redirect links with "/redcap_latest/" instead of a version... + #RewriteCond %{REQUEST_URI} "^.*\/redcap_latest\/.*$" + #RewriteRule "^(.+)$" "/redcap_redirect.php" [PT,L,NS] + ``` diff --git a/redcap_redirect.php b/redcap_redirect.php index c5b1746..1d945d8 100644 --- a/redcap_redirect.php +++ b/redcap_redirect.php @@ -24,6 +24,10 @@ RewriteCond %{REQUEST_URI} "^.*\/redcap_v(\d+\.\d+\.\d+)\/.*$" # Redirect to this script to handle the version substitution RewriteRule "^(.+)$" "/redcap_redirect.php" [PT,L,NS] + + #Optional: Uncomment to also redirect links with "/redcap_latest/" instead of a version... + #RewriteCond %{REQUEST_URI} "^.*\/redcap_latest\/.*$" + #RewriteRule "^(.+)$" "/redcap_redirect.php" [PT,L,NS] * Note: if your redcap server is not installed in the DOCUMENT_ROOT, then you may need to modify the RewriteRule line @@ -44,16 +48,22 @@ $requestUri = filter_var($_SERVER['REQUEST_URI'], FILTER_SANITIZE_URL); -// Check the redirectURL for a redcap version - https://regex101.com/r/q7oCR5/1 -$re = '/^(.*\/redcap_v)(\d+\.\d+\.\d+)(\/.*?)(\?.*)*$/'; +// Check the redirectURL for a redcap version or the string "redcap_latest" - https://regex101.com/r/q7oCR5/1 +$re = '/^(.*\/redcap_)(v\d+\.\d+\.\d+)(\/.*?)(\?.*)*$/'; preg_match($re, $requestUri, $uriMatches); $uriVersion = empty($uriMatches[2]) ? NULL : $uriMatches[2]; -// See if we have a version in the url and it is not current -if (!empty($uriVersion) && ($uriVersion !== $redcap_version)) { +if (empty($uriMatches)) { + $re = '/^(.*\/redcap_)(latest)(\/.*?)(\?.*)*$/'; + preg_match($re, $requestUri, $uriMatches); + $uriVersion = empty($uriMatches[2]) ? NULL : $uriMatches[2]; +} + +// See if we have a version in the url and it is not current (or the string "latest") +if ((!empty($uriVersion) && ($uriVersion !== $redcap_version)) || $uriVersion == 'latest') { // Rebuild the new url with the current db version - $newUrl = $uriMatches[1] . $redcap_version . $uriMatches[3]; + $newUrl = $uriMatches[1] . "v" . $redcap_version . $uriMatches[3]; // Build the server path to the url $newUrlPath = $_SERVER['DOCUMENT_ROOT'] . $newUrl;