• Stars
    star
    168
  • Rank 225,507 (Top 5 %)
  • Language
    PHP
  • License
    Other
  • Created over 8 years ago
  • Updated almost 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

DEPRECATED Retour allows you to intelligently redirect legacy URLs, so that you don't lose SEO value when rebuilding & restructuring a website.

No Maintenance Intended

DEPRECATED

This Craft CMS 2.x plugin is no longer supported, but it is fully functional, and you may continue to use it as you see fit. The license also allows you to fork it and make changes as needed for legacy support reasons.

The Craft CMS 3.x version of this plugin can be found here: craft-retour and can also be installed via the Craft Plugin Store in the Craft CP.

Retour plugin for Craft CMS

Retour allows you to intelligently redirect legacy URLs, so that you don't lose SEO value when rebuilding & restructuring a website.

Screenshot

Related: Retour for Craft 3.x

Installation

To install Retour, follow these steps:

  1. Download & unzip the file and place the retour directory into your craft/plugins directory
  2. -OR- do a git clone https://github.com/nystudio107/retour.git directly into your craft/plugins folder. You can then update it with git pull
  3. -OR- install with Composer via composer require nystudio107/retour
  4. Install plugin in the Craft Control Panel under Settings > Plugins
  5. The plugin folder should be named retour for Craft to see it. GitHub recently started appending -master (the branch name) to the name of the folder for zip file downloads.

Retour works on Craft 2.4.x, Craft 2.5.x, and Craft 2.6.x.

Retour Overview

Retour allows you to intelligently redirect legacy URLs, so that you don't lose SEO value when rebuilding & restructuring a website.

In addition to supporting traditional exact and RegEx matching of URL patterns, Retour also has a Retour Redirect FieldType that you can add to your entries. This allows you to have dynamic entry redirects that have access to the data in your entries when matching URL patterns.

Retour will also automatically create a redirect for you if you change an entry's slug, or move an entry around in a Structure.

Retour is written to be performant. There is no impact on your website's performance until a 404 exception happens; and even then the resulting matching happens with minimal impact.

Don't just rebuild a website. Transition it with Retour.

Why Use a Plugin for Redirects?

If you have just a few static redirects, then your best bet is to put them in your .htaccess file, or better yet, in your .conf file for your virtual host. However, there are a number of cases where using a plugin to handle it is a better solution:

  1. If you have a large number of redirects, it will slow down every single request your web server handles unnecessarily if they are in .htaccess or .conf
  2. Often the URL patterns from the legacy website do not match the new website URLs in a deterministic way, which makes creating redirects difficult
  3. Sometimes you don't have access to the server config files, or you want to give your client the ability to manage redirects easily

Retour solves these problems:

  1. Retour only attempts to do a redirect after the web server has already thrown a 404 exception. Once a redirect mapping is successfully determined, it also caches the result for speedy resolution of the next redirect request.
  2. Retour also gives you the ability to do Dynamic Entry Redirects that allow you to import a piece of legacy data into your entries to use as a key for determining the new URL mapping. In this way, utterly dissimilar URLs can be mapped for redirection effectively.
  3. It provides an easy to use GUI that the client can use from Craft's AdminCP, and keeps statistics on the 404 hits (and misses)

A Word about .htaccess

People using the Apache webserver are familiar with the .htaccess file, and may even be using it for redirects. It's very likely that you should not be using .htaccess at all; instead you should disable .htaccess via AllowOverride none and make your configuration changes in your webserver configuration files. From Apache HTTP Server Tutorial: .htaccess files

There are two main reasons to avoid the use of .htaccess files.

The first of these is performance. When AllowOverride is set to allow the
use of .htaccess files, httpd will look in every directory for .htaccess
files. Thus, permitting .htaccess files causes a performance hit, whether or
not you actually even use them! Also, the .htaccess file is loaded every
time a document is requested.

Further note that httpd must look for .htaccess files in all higher-level
directories, in order to have a full complement of directives that it must
apply. (See section on how directives are applied.) Thus, if a file is
requested out of a directory /www/htdocs/example, httpd must look for the
following files:

/.htaccess
/www/.htaccess
/www/htdocs/.htaccess
/www/htdocs/example/.htaccess

And so, for each file access out of that directory, there are 4 additional
file-system accesses, even if none of those files are present. (Note that
this would only be the case if .htaccess files were enabled for /, which is
not usually the case.)

In the case of RewriteRule directives, in .htaccess context these regular
expressions must be re-compiled with every request to the directory, whereas
in main server configuration context they are compiled once and cached.
Additionally, the rules themselves are more complicated, as one must work
around the restrictions that come with per-directory context and
mod_rewrite. Consult the Rewrite Guide for more detail on this subject.

As you can see, avoiding the use of .htaccess completely is best if at all possible, and especially avoid it for RewriteRule directives, such as 404 rewrites.

Dynamic Entry Redirects

Retour implements a Retour Redirect FieldType that you can add to your Entry Types. Retour will look for 404 (Not Found) URLs that match the Legacy URL Pattern, and redirect them to this entry's URL.

You also get the context of the entry that you can use when matching legacy URLs; so if you've imported a field called recipeid into your new website, you can the Retour Redirect FieldType look for it in your Legacy URL Pattern, e.g.: /old-recipes/{recipeid}

This allows you to key off of a piece of legacy data that was imported, for the cases when the new URL patterns don't look anything like the Legacy URL Patterns, or follow any pattern that RegEx is useful for matching.

Screenshot

Creating a Retour Redirect Field

Create a Retour Redirect field as you would any other field; then set the default values for it. For new entries, it will default to the values entered here, so you can put your matching pattern in once, rather than having to do it for each entry.

  • Default Legacy URL Pattern - Enter the URL pattern that Retour should match. This matches against the path, the part of the URL after the domain name. You can include tags that output entry properties, such as {title} or {myCustomField} in the text field below. e.g.: Exact Match: /recipes/{recipeid} or RegEx Match: .*RecipeID={recipeid}$ where {recipeid} is a field handle to a field in this entry.
  • Default Pattern Match Type - What type of matching should be done with the Legacy URL Pattern. Details on RegEx matching can be found at regexr.com. If a plugin provides a custom matching function, you can select it here.
  • Default Redirect Type - Select whether the redirect should be permanent or temporary.
  • Redirect Changeable - Whether to allow the user to change the redirect while editing the entry.

Configuring a Retour Redirect Field

  • Legacy URL Pattern - Enter the URL pattern that Retour should match. This matches against the path, the part of the URL after the domain name. You can include tags that output entry properties, such as {title} or {myCustomField} in the text field below. e.g.: Exact Match: /recipes/{recipeid} or RegEx Match: .*RecipeID={recipeid}$ where {recipeid} is a field handle to a field in this entry.
  • Pattern Match Type - What type of matching should be done with the Legacy URL Pattern. Details on RegEx matching can be found at regexr.com. If a plugin provides a custom matching function, you can select it here.
  • Redirect Type - Select whether the redirect should be permanent or temporary.

Note: if you add a Retour Redirect FieldType to an existing Section, or you import data from a foreign source into a Section with a Retour Redirect FieldType, the default values you set for the Retour Redirect FieldType will not be propagated to the entry yet. To cause that to happen, go to Settings->Sections then click on the Section to edit it, and hit Save. This will cause all of the entries in that section to be re-saved, and Retour will fill in the default field values.

Static Redirects

Manually Creating Static Redirects

Static Redirects are useful when the Legacy URL Patterns and the new URL patterns are deterministic. You can create them by clicking on Retour->Redirects and then clicking on the + New Static Redirect button.

  • Legacy URL Pattern - Enter the URL pattern that Retour should match. This matches against the path, the part of the URL after the domain name. e.g.: Exact Match: /recipes/ or RegEx Match: .*RecipeID=(.*)
  • Destination URL - Enter the destination URL that should be redirected to. This can either be a fully qualified URL or a relative URL. e.g.: Exact Match: /new-recipes/ or RegEx Match: /new-recipes/$1
  • Pattern Match Type - What type of matching should be done with the Legacy URL Pattern. Details on RegEx matching can be found at regexr.com. If a plugin provides a custom matching function, you can select it here.
  • Redirect Type - Select whether the redirect should be permanent or temporary.

Importing an Existing .htaccess file

Retour also allows you to import an existing .htaccess file and all of the redirects it contains into Retour by clicking on Retour->Redirects and then clicking on the Import .htaccess File button.

It will import redirects from Redirect and RedirectMatch directives in the file. It will ignore RewriteRules because they don't necessarily have a 1:1 mapping, you can have several RewriteRules that are strung together to figure out the final redirect.

It asks your browser to look for only text files to upload; if the .htaccess file you have isn't a .txt file, you can force it to allow you to upload it by choosing Format: All Files.

Renamed Slug Redirects

If you rename an entry's slug (and the Section the entry is in has URLs), Retour will automatically create a static redirect for you to keep traffic going to the right place. It will also automatically create a static redirect if you move an entry around in a Structure.

It will appear listed under the "Static Redirects" section like any other static redirect.

Retour Statistics

Retour keeps track of every 404 your website receives. You can view them by clicking on Retour->Statistics.

Only one record is saved per URL Pattern, so the database won't get clogged with a ton of records.

Retour Widget

If you'd like to see an overview of the Retour Statistics in your dashboard, you can add a Retour widget to your Dashboard:

Screenshot

It displays the total number of handled and not handled 404s, and the 5 most recent 404 URLs in each category right in your dashboard.

Developer Info

Custom Match Functions via Plugin

Retour allows you to implement a custom matching function via plugin, if the Exact and RegEx matching are not sufficient for your purposes.

In your main plugin class file, simply add this function:

/**
 * retourMatch gives your plugin a chance to use whatever custom logic is needed for URL redirection.  You are passed
 * in an array that contains the details of the redirect.  Do whatever matching logic, then return true if is a
 * matched, false if it is not.
 *
 * You can alter the 'redirectDestUrl' to change what URL they should be redirected to, as well as the 'redirectHttpCode'
 * to change the type of redirect.  None of the changes made are saved in the database.
 *
 * @param mixed An array of arguments that define the redirect
 *            $args = array(
 *                'redirect' => array(
 *                    'id' => the id of the redirect record in the retour_redirects table
 *                    'associatedElementId' => the id of the entry if this is a Dynamic Entry Redirect; 0 otherwise
 *                    'redirectSrcUrl' => the legacy URL as entered by the user
 *                    'redirectSrcUrlParsed' => the redirectSrcUrl after it has been parsed as a micro template for {variables}
 *                        via renderObjectTemplate().  This is typically what you would want to match against.
 *                    'redirectMatchType' => the type of match; this will be set to your plugin's ClassHandle
 *                    'redirectDestUrl' => the destination URL for the entry this redirect is associated with, or the
 *                        destination URL that was manually entered by the user
 *                    'redirectHttpCode' => the redirect HTTP code (typically 301 or 302)
 *                    'hitCount' => the number of times this redirect has been matched, and the redirect done in the browser
 *                    'hitLastTime' => the date and time of the when this redirect was matched
 *                    'locale' => the locale of this redirect
 *                )
 *            );
 * @return bool Return true if it's a match, false otherwise
 */
public function retourMatch($args)
{
    return true;
}

Your plugin will then appear in the list of Pattern Match Types that can be chosen from via Retour->Redirects or via the Retour Redirect FieldType.

Utility Functions

craft.retour.getHttpStatus in your templates will return the HTTP Status code for the current template, so you can display a special message for people who end up on a page via a 301 or 302 redirect.

Retour Roadmap

Some things to do, and ideas for potential features:

  • Craft 3 compatibility
  • Add the ability to mass-import redirects from a CSV file

Brought to you by nystudio107

More Repositories

1

annotated-webpack-config

This is the companion github repo for the "An Annotated webpack 4 Config for Frontend Web Development" article.
JavaScript
434
star
2

craft-scripts

Shell scripts to manage database backups, asset backups, file permissions, asset syncing, cache clearing, and database syncing between Craft CMS environments
Shell
364
star
3

seomatic

DEPRECATED A turnkey SEO implementation for Craft CMS 2.x that is comprehensive, powerful, and flexible
PHP
357
star
4

nginx-craft

An Nginx virtual host configuration for Craft CMS that implements a number of best-practices.
307
star
5

craft

nystudio107 Craft 3 CMS scaffolding project
Twig
289
star
6

craft-imageoptimize

Automatically create & optimize responsive image transforms, using either native Craft transforms or a service like Imgix, with zero template changes.
PHP
235
star
7

craft-seomatic

SEOmatic facilitates modern SEO best practices & implementation for Craft CMS 3. It is a turnkey SEO system that is comprehensive, powerful, and flexible.
PHP
163
star
8

minify

DEPRECATED A simple plugin that allows you to minify blocks of HTML, CSS, and JS inline in Craft CMS templates
PHP
107
star
9

instantanalytics

DEPRECATED Instant Analytics brings full Google Analytics support to your Twig templates and automatic Craft Commerce integration with Google Enhanced Ecommerce.
PHP
101
star
10

craft-twigpack

Twigpack is a bridge between Twig and webpack, with manifest.json & webpack-dev-server HMR support
PHP
97
star
11

rollup-plugin-critical

Vite.js & Rollup plugin for generating critical CSS
TypeScript
88
star
12

devmode

Website for the devMode.fm podcast
Twig
85
star
13

craft-multi-environment

Efficient and flexible multi-environment config for Craft CMS
PHP
83
star
14

vitejs-docker-dev

Local development environment for developing vite.js via Docker container
Makefile
80
star
15

generator-craftplugin

generator-craftplugin is a Yeoman generator for Craft CMS plugins
PHP
73
star
16

craft3-multi-environment

DEPRECATED Efficient and flexible multi-environment config for Craft CMS
72
star
17

craft-typogrify

Typogrify prettifies your web typography by preventing ugly quotes and 'widows' and more
PHP
70
star
18

cookies

DEPRECATED Secure Cookies for Twig Templates in Craft CMS
PHP
68
star
19

generator-craftinstall

DEPRECATED generator-craftinstall is a Yeoman generator for Craft CMS installs
JavaScript
57
star
20

craft-vite

Allows the use of the Vite.js next generation frontend tooling with Craft CMS
PHP
50
star
21

buffer

DEPRECATED Send social updates to Twitter, Facebook, etc. through Buffer.com via Twig templates, URLs, and plugins.
PHP
44
star
22

richvariables

DEPRECATED Allows you to easily use Craft Globals as variables in Rich Text fields
PHP
44
star
23

craft-transcoder

Transcode video & audio files to various formats, and provide video thumbnails
PHP
43
star
24

craft-autocomplete

Provides Twig template IDE autocomplete of Craft CMS & plugin variables
PHP
41
star
25

recipe

DEPRECATED A comprehensive recipe FieldType for Craft CMS that includes metric/imperial conversion, portion calculation, and JSON-LD microdata support
PHP
38
star
26

craft-cookies

A simple plugin for setting and getting cookies from within Craft CMS templates.
PHP
37
star
27

craft-retour

Retour allows you to intelligently redirect legacy URLs, so that you don't lose SEO value when rebuilding & restructuring a website
PHP
36
star
28

craft-minify

A simple plugin that allows you to minify blocks of HTML, CSS, and JS inline in Craft CMS templates
PHP
36
star
29

twig-bundle-installer

Install, update, and manage Twig template bundles via Composer
PHP
35
star
30

dotenvy

Speed up your production sites by ditching .env for key/value variable pairs as Apache, Nginx, and shell equivalents
PHP
32
star
31

spin-up-craft

Spin up a working Craft CMS instance quickly & easily with Docker
Dockerfile
31
star
32

craft-routemap

Returns a list of Craft/Vue/React route rules and element URLs for ServiceWorkers from Craft entries
PHP
31
star
33

craft-recipe

A comprehensive recipe FieldType for Craft CMS that includes metric/imperial conversion, portion calculation, and JSON-LD microdata support
PHP
28
star
34

routemap

DEPRECATED Returns a list of Craft/Vue/React route rules and entry & asset URLs for ServiceWorkers from Craft entries
PHP
28
star
35

craft-similar

Similar for Craft lets you find elements, Entries, Categories, Commerce Products, etc, that are similar, based on... other related elements.
PHP
26
star
36

disqus

DEPRECATED Integrate the Disqus commenting system into Craft CMS websites, including Single Sign On (SSO) and custom login/logout URLs.
PHP
26
star
37

craft-emptycoalesce

Empty Coalesce adds the ??? operator to Twig that will return the first thing that is defined, not null, and not empty
PHP
26
star
38

eagerbeaver

DEPRECATED Allows you to eager load elements from auto-injected Entry elements on demand from your templates.
PHP
25
star
39

transcoder

DEPRECATED Transcode videos to various formats, and provide thumbnails of the video
PHP
25
star
40

craft-richvariables

Allows you to easily use Craft Globals as variables in Rich Text fields
PHP
25
star
41

craft-webperf

Webperf helps you build & maintain high quality websites through Real User Measurement of your website's performance
PHP
25
star
42

plugindev

nystudio107 Craft CMS plugin development environment scaffolding
PHP
23
star
43

fastcgicachebust

DEPRECATED Bust the Nginx FastCGI Cache when entries are saved or created.
PHP
22
star
44

craft-closure

Allows you to use arrow function closures in Twig
PHP
22
star
45

craft-twigprofiler

Twig Profiler allows you to profile sections of your Twig templates, and see the resulting timings in the Yii2 Debug Toolbar
PHP
21
star
46

craft-templatecomments

Adds a HTML comment with performance timings to demarcate `{% block %}`s and each Twig template that is included or extended.
PHP
20
star
47

craft-instantanalytics

Instant Analytics brings full Google Analytics support to your Twig templates and automatic Craft Commerce integration with Google Enhanced Ecommerce.
PHP
20
star
48

pathtools

DEPRECATED This twig plugin for the Craft CMS brings convenient path & url manipulation functions & filters to your Twig templates.
PHP
20
star
49

craft-fastcgicachebust

Bust the Nginx FastCGI Cache when entries are saved or created.
PHP
20
star
50

spin-up-statamic

Spin up a working Statamic instance quickly & easily with Docker
PHP
18
star
51

site-module

An example module for Craft CMS 3 that lets you enhance your websites with a custom site module
PHP
18
star
52

save-remote-file-webpack-plugin

webpack 4 plugin to download remote files, and save them locally
JavaScript
17
star
53

craft-youtubeliveembed

This plugin allows you to embed a YouTube live stream and/or live chat on your webpage
PHP
17
star
54

craft-connect

Allows you to connect to external databases and perform db queries
PHP
16
star
55

craft-twigfield

DEPRECATED: Provides a twig editor field with Twig & Craft API autocomplete
PHP
15
star
56

craft-eagerbeaver

DEPRECATED: Allows you to eager load elements from auto-injected Entry elements on demand from your templates.
PHP
14
star
57

tailwind-css-performance

Small repo to demonstrate the slow building of Tailwind CSS using `webpack-dev-server` HMR & PostCSS
JavaScript
13
star
58

docker-images

Docker image source files
Dockerfile
13
star
59

disclosedassets

DEPRECATED Allow your clients to find asset sub-folders by disclosing them by default
PHP
13
star
60

craft-disqus

Integrates the Disqus commenting system into Craft 3 websites, including Single Sign On (SSO) and custom login/logout URLs
PHP
12
star
61

vanillaforums

DEPRECATED Single Sign On plugin for VanillaForums/jsConnect and CraftCMS
PHP
11
star
62

craft-pathtools

This twig plugin for the Craft CMS brings convenient path & url manipulation functions & filters to your Twig templates.
PHP
11
star
63

craft-plugin-vite

Plugin Vite is the conduit between Craft CMS plugins and Vite, with manifest.json & HMR support
PHP
10
star
64

craft-units

Units is a plugin that can convert between any units of measure, and comes with a Field for content authors to use
PHP
9
star
65

craft-icalendar

Tools for parsing & formatting the RFC 2445 iCalendar (.ics) specification
PHP
8
star
66

gatsby-concurrent-mode

Enable Concurrent Mode in in React for Gatsby projects.
JavaScript
8
star
67

charcuterie-vue-vitest

charcuterie-vue-vitest
TypeScript
6
star
68

get-webpack-config

Utilities to help you modularize your webpack configs
JavaScript
6
star
69

craft-plugin-vite-buildchain

A drop-in buildchain for using Vite in your Craft CMS plugins or modules
TypeScript
6
star
70

create-symlink-webpack-plugin

webpack 4 plugin to generate symlinks
JavaScript
5
star
71

spin-up-statamic-starter

Starter kit to add Spin Up Statamic to your existing Statamic project
PHP
4
star
72

craft-code-field

Provides a Code Field that has a full-featured code editor with syntax highlighting & autocomplete
PHP
4
star
73

craft-code-editor

Provides a code editor field with Twig & Craft API autocomplete
PHP
4
star
74

craft-plugin-manifest

Plugin Manifest is the conduit between Craft CMS plugins and webpack, with manifest.json & webpack-dev-server HMR support
PHP
4
star
75

matrixfacades

This is a project demonstrates how to improve authoring experience Matrix Façades
PHP
4
star
76

.github

3
star
77

craft-vanillaforums

Single Sign On plugin for Vanilla Forums/jsConnect and CraftCMS
PHP
3
star
78

vitepress-starter

Starter repository for VitePress documenation
3
star
79

craft-twig-sandbox

Allows you to easily create a sandboxed Twig environment where you can control what tags, filters, functions, and object methods/properties are allowed
PHP
3
star
80

craft-twig-base-templates

Craft CMS Twig base templates
Twig
3
star
81

craft-instantanalytics-ga4

Instant Analytics brings full Google GA4 server-side analytics support to your Twig templates and automatic Craft Commerce integration
PHP
3
star
82

craft-imageoptimize-thumbor

Provides an Thumbor image transform type for the ImageOptimize plugin
PHP
2
star
83

craft-imageoptimize-imgix

Provides an Imgix image transform type for the ImageOptimize plugin.
PHP
2
star
84

docker-yeoman

node yeoman Docker images
Dockerfile
2
star
85

craft-imageoptimize-sharp

Provides an Sharp image transform type for the ImageOptimize plugin
PHP
2
star
86

fwt

Website for the Friends of Webster Trails website http://webstertrails.org/
Twig
2
star
87

test-twig-bundle

Test bundle of Twig templates for Bundle Installer
Twig
2
star
88

schemagen

Generates PHP models representing schema.org JSON-LD types
PHP
2
star
89

entitydecode

DEPRECATED Decodes HTML Entities
PHP
1
star
90

spin-up-statamic-peak

Spin up a working Statamic Peak quickly & easily with Docker
PHP
1
star
91

srcsetdev

TypeScript
1
star
92

craft-twig-base-components

Craft CMS Twig base components
Twig
1
star