Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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]

</IfModule>
```

Expand Down
20 changes: 15 additions & 5 deletions redcap_redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]
</IfModule>

* Note: if your redcap server is not installed in the DOCUMENT_ROOT, then you may need to modify the RewriteRule line
Expand All @@ -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;
Expand Down