Skip to content

Commit

Permalink
Merge pull request #87 from UTCGilligan/feature/UTCT-86-404-if-not-found
Browse files Browse the repository at this point in the history
Feature/utct 86 404 if not found
  • Loading branch information
UTCGilligan authored Apr 23, 2024
2 parents 131e8be + 7676985 commit e1a4956
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 17 deletions.
27 changes: 27 additions & 0 deletions 404.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php include 'frontend/header.php'; ?>

<body>

<div class="container-fluid h-100">
<div class="row justify-content-center align-items-center h-100">
<div class="col-12 col-lg-10 col-xl-8 col-xxl-5 mt-5">
<div class="card border-0 mt-5">
<div class="text-center">
<img src="frontend/assets/svg/utc-wordmark-reverse.svg" alt="UTC Logo" width="300px" style="margin-top:-4rem;">
</div>
<div class="card-body px-md-5">
<h1>Sorry,</h1>
<p>You have followed a shortcut link to a path that does not exist.</p>
<h2>Options</h2>
<ul>
<li>Go to the <a href="https://www.utc.edu">UTC home page</a></li>
<li>Try to <a href="#" onclick="window.close();">close this window</a></li>
</ul>

</div>
</div>
</div>
</div>
</div>
</body>
</html>
42 changes: 25 additions & 17 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions user/plugins/404-if-not-found/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004

Copyright (C) 2004 Sam Hocevar <[email protected]>

Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

0. You just DO WHAT THE FUCK YOU WANT TO.

22 changes: 22 additions & 0 deletions user/plugins/404-if-not-found/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# YOURLS plugin : 404 if not found [![Listed in Awesome YOURLS!](https://img.shields.io/badge/Awesome-YOURLS-C5A3BE)](https://github.com/YOURLS/awesome-yourls/)

By default and by design, when a requested short URL is not found, YOURLS redirects to the site root with a "302 temporary redirect" header.

From the source:
```php
yourls_redirect( YOURLS_SITE, 302 ); // no 404 to tell browser this might change,
// and also to not pollute logs
```

This plugin outputs a default "`404 not found`" error page instead.

## Installation

1. In `/user/plugins`, create a new folder named `404-if-not-found`.
2. Drop these files in that directory.
3. Go to the Plugins administration page (eg. `http://sho.rt/admin/plugins.php`) and activate the plugin.
4. Have fun!

## License

Do whatever the hell you want with this.
48 changes: 48 additions & 0 deletions user/plugins/404-if-not-found/plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/*
Plugin Name: 404 if not found
Plugin URI: https://github.com/YOURLS/404-if-not-found/
Description: Send a 404 (instead of a 302) when short URL is not found
Version: 1.2
Author: Ozh
Author URI: https://yourls.org/
*/

// No direct call
if( !defined( 'YOURLS_ABSPATH' ) ) die();

// 'keyword' provided ('abc' in 'http://sho.rt/abc') but not found
yourls_add_action('redirect_keyword_not_found', 'ozh_404_if_not_found', 999);

// 'keyword+' provided but this isnt an existing stat page
yourls_add_action('infos_keyword_not_found', 'ozh_404_if_not_found', 999);

// 'keyword' not provided: direct attempt at http://sho.rt/yourls-go.php or -infos.php
yourls_add_action('redirect_no_keyword', 'ozh_404_if_not_found', 999);
yourls_add_action('infos_no_keyword', 'ozh_404_if_not_found', 999);


/**
* if you want to display a custom 404 page instead, replace the default function with
* the following :
*/
function ozh_404_if_not_found() {
yourls_status_header(404);
include_once($_SERVER['DOCUMENT_ROOT'].'/404.php'); // full path to your error document
die();
}

// Display a default 404 not found page
/**
* function ozh_404_if_not_found() {
* yourls_status_header(404);
* $notfound = '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">'
* . '<html><head>'
* . '<title>404 Not Found</title>'
* . '</head><body>'
* . '<h1>Not Found</h1>'
* . '<p>The requested URL was not found on this server.</p>'
* . '</body></html>';
* die($notfound);
* }
*/

0 comments on commit e1a4956

Please sign in to comment.