Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/fork/craft-here

Use the SEO plugin redirects to write nginx and apache redirect map config files (perfect for headless Craft CMS Setups)
https://github.com/fork/craft-here

apache craftcms headless nginx redirect routing

Last synced: about 20 hours ago
JSON representation

Use the SEO plugin redirects to write nginx and apache redirect map config files (perfect for headless Craft CMS Setups)

Awesome Lists containing this project

README

        



**Table of contents**

- [Features](#features)
- [Requirements](#requirements)
- [Setup](#setup)
- [Usage](#usage)
- [Roadmap](#roadmap)

---

## Features

- Manage custom redirects directly within the server configuration (for Nginx and Apache)
- Perfectly suited for headless Craft CMS Setups
- Use the redirects features and UI of the [Craft SEO Plugin](https://github.com/ethercreative/seo)

## Requirements

- Craft CMS 4
- [Craft SEO Plugin](https://github.com/ethercreative/seo)

## Setup

**1. Install**

Install the package

```sh
cd /path/to/project
composer require fork/craft-here
```

**2. Configuration file**

- Copy the example `config.php` to your Craft config directory and rename it to `redirects.php`
- Specify the server type (and a reload command if you use nginx). Here's an example:

```php
[
'serverType' => 'nginx' // or 'apache'
],

// Dev environment settings
'dev' => [
//'redirectsReloadCommand' => 'my-command',
],

// Staging environment settings
'staging' => [
],

// Production environment settings
'production' => [
//'redirectsReloadCommand' => 'sudo /etc/init.d/nginx reload',
],
];

```

In your server configuration include the redirect map files (which will be created after plugin has been installed):

```
# NGINX EXAMPLE:

# see https://serverfault.com/a/890715/487169 for why we use "[.]" instead of a regular period "."
include /var/www/html/redirects/my.domain.com/redirects-301[.]map;
include /var/www/html/redirects/my.domain.com/redirects-302[.]map;

# 301 MOVED PERMANENTLY
if ($redirect_moved = false) {
set $redirect_moved "";
}
if ($redirect_moved != "") {
rewrite ^(.*)$ $redirect_moved permanent;
}
# 302 FOUND (aka MOVED TEMPORARILY)
if ($redirect_found = false) {
set $redirect_found "";
}
if ($redirect_found != "") {
rewrite ^(.*)$ $redirect_found redirect;
}
```

```
# APACHE EXAMPLE:

RewriteEngine On
RewriteMap redirects-301 txt:/var/www/html/redirects/my.domain.com/redirects-301.map
RewriteMap redirects-302 txt:/var/www/html/redirects/my.domain.com/redirects-302.map

RewriteCond ${redirects-301:%{REQUEST_URI}} ^.+$
RewriteRule .* https://${redirects-301:%{HTTP_HOST}%{REQUEST_URI}} [redirect=permanent,last]

RewriteCond ${redirects-302:%{REQUEST_URI}} ^.+$
RewriteRule .* https://${redirects-302:%{HTTP_HOST}%{REQUEST_URI}} [redirect=temp,last]
```

## Usage

Once the plugin has been installed it will create all necessary redirect map files which need to be included into the server config.
After that just use the SEO Plugin UI to manage your redirects.

## Roadmap

- [ ] Settings maybe (instead of config file)

---


Fork Logo

Brought to you by Fork Unstable Media GmbH