项目作者: autologic-web

项目描述 :
A lazy Symfony bundle for handling 301 redirection using event listeners and regex
高级语言: PHP
项目地址: git://github.com/autologic-web/redirect-bundle.git
创建时间: 2017-10-12T15:47:20Z
项目社区:https://github.com/autologic-web/redirect-bundle

开源协议:MIT License

下载


Symfony Redirect Bundle

Build Status StyleCI Maintainability Test Coverage

Configure redirections after a migration or structural changes to your app/website.

It catches exception events, if they are of type NotFoundHttpException it will look for a configured rule and return a RedirectResponse response to redirect the user.

Works for Symfony ^2.7, ^3.0 or ^4.0 with PHP ^5.6 or ^7.0

It’s been designed to be as unobtrusive as possible since the need to do this sort of thing is often temporary - Google recommends leaving them in place for a year. Just include the bundle and add a block of configuration for your redirect rules.

Installation

Install via Composer

  1. $ composer require autologic-web/redirect-bundle

Symfony < 4

Include the bundle in AppKernel.php

  1. # app/AppKernel.php
  2. /**
  3. * Class AppKernel
  4. */
  5. class AppKernel extends Kernel
  6. {
  7. /**
  8. * @return array
  9. */
  10. public function registerBundles()
  11. {
  12. $bundles = [
  13. // All your bundles
  14. new Autologic\Bundle\RedirectBundle\AutologicRedirectBundle(),
  15. ];
  16. return $bundles;
  17. }
  18. }

Symfony 4

Include the bundle in bundles.php

  1. # config/bundles.php
  2. return [
  3. // All your bundles
  4. Autologic\Bundle\RedirectBundle\AutologicRedirectBundle::class => ['all' => true],
  5. ];

Configuration

Basic Usage

  1. # app/config.yml
  2. autologic_redirect:
  3. rules:
  4. - { pattern: '/old-route/', redirect: 'domain.com/new-route' }

pattern (string, required)

Use regular expressions to match the full URI being requested. The service catches 404 exceptions, uses preg_match to find a rule matching the missing page’s URI before throwing the 404 in the event it cannot match.

redirect (string, required)

The fully qualified redirect URI. The bundle will set the protocol (http/https) based on the incoming original request so it ports from dev to prod easily.

status (int, optional)

Set the status code (default: 301) for the redirection. Tip: use 302 while debugging to avoid 301 permanent redirects from being cached in the browser.

forwarding (bool, optional)

Append the original route to the redirection (default: false). Useful in the case that other services/applications have their own redirect logic in place or route structure is the same on a different domain or path.

absolute (bool, optional)

Force absolute or relative redirects (default: null/auto). If left unset, it will detect a hostname in the redirect and either use the original request host if the redirect does not contain a host or use the redirect verbatim if it does.

protocol (string, optional)

Force the redirect protocol (default: null/auto). If left unset, it will detect the protocol from the original request and use that.

Other Examples

  1. # app/config.yml
  2. autologic_redirect:
  3. rules:
  4. # custom status code
  5. - { pattern: '/old-route/', redirect: 'domain.com/new-route', status: 302 }
  6. # forwarding: this will redirect to domain.com/new-route/old-route
  7. - { pattern: '/old-route/', redirect: 'domain.com/new-route', forwarding: true }
  8. # absolute: will force relative or absolute redirects
  9. # if false it will redirect to the route on the current host
  10. - { pattern: '/old-route/', redirect: '/new-route', absolute: false }
  11. # protocol: will force the protocol
  12. - { pattern: '/old-route/', redirect: '/new-route', protocol: 'ftp://' }
  13. # priority: this first rule will match first when a user visits /old-route/sub-route, the second acting as a fallback
  14. - { pattern: '/.*old-route\/sub-route', redirect: 'domain.com/new-route/sub-route' }
  15. - { pattern: '/.*old-route/', redirect: 'domain.com/new-route' }
  16. # match subdomains and more complex patterns and use parameters
  17. - { pattern: '/au\..+?\.[^\/]+.*blog\/old-australian-blog-post-on-any-domain-of-subdomain/',
  18. redirect: 'au.%base_domain%/news/new-australian-news-article' }

Logging

To enable logging of unmatched 404 errors, just inject a logger into the listener service in your services.yml:

  1. # app/services.yml
  2. services:
  3. autologic_redirect.event.redirect_listener:
  4. class: Autologic\Bundle\RedirectBundle\Event\RedirectListener
  5. arguments:
  6. - '@autologic_redirect.service.redirect_service'
  7. - '@logger'
  8. tags:
  9. - { name: kernel.event_listener, event: kernel.exception }

This will log at notice level to help sniff out 404s that don’t have any redirection rules in place.