terminal42/contao-url-rewrite

Contao开源内容管理系统(CMS)的URL重写工具包

支持包维护!
terminal42
其他

安装数: 64,116

依赖项: 2

建议者: 0

安全性: 0

星标: 15

关注者: 9

分支: 4

公开问题: 1

类型:contao-bundle

1.7.5 2023-09-22 07:43 UTC

README

该扩展为Contao提供了一种新的设置各种URL重写的方法。可用的配置提供者是

  • 包配置提供者 - 条目来自 config.yml 文件
  • 数据库提供者 - 条目来自后端模块

幕后,规则被添加为内部应用程序路由器中的路由,这允许使用Symfony路由组件提供的所有功能。

安装

通过Composer安装包

composer require terminal42/contao-url-rewrite

短URL迁移

由于扩展 fritzmg/contao-short-urls 已被弃用,您可以使用扩展 bwein-net/contao-migrate-short-urls 将短URL迁移到URL重写。

配置

包配置

包配置是可选的。在这里,您可以定义条目并禁用后端管理模块。

注意:如果您想使用 % 字符,请确保它已通过重复它 %% 正确转义。

# config/config.yml
terminal42_url_rewrite:
    backend_management: false # Disable backend management of entries (true by default)
    entries: # Optional entries
        -
            request: { path: 'find/{address}' }
            response: { code: 303, uri: 'https://www.google.com/maps?q={address}' }

        -
            request:
                path: 'news/{news}'
                requirements: {news: '\d+'}
            response:
                code: 301
                uri: '{{news_url::{news}|absolute}}'

        -
            request:
                path: 'home.php'
                hosts: ['localhost']
                condition: "context.getMethod() == 'GET' and request.query.has('page')"
            response:
                uri: '{{link_url::{page}|absolute}}'

在非Contao管理版下运行

如果您正在运行Contao管理版,则该扩展应该直接可用。对于所有其他系统,您还必须在配置文件中注册路由配置。

# config/routing.yml
imports:
  - { resource: '@Terminal42UrlRewriteBundle/Resources/config/routing.yml' }

示例

  1. 在谷歌地图上查找地址
Type: basic
Path restriction: find/{address}
Response code: 303 See Other
Response URI: https://www.google.com/maps?q={address}
---
Result: domain.tld/find/Switzerland → https://www.google.com/maps?q=Switzerland
  1. 重定向到特定的新闻条目
Type: basic
Path restriction: news/{news}
Requirements: [news => \d+]
Response code: 301 Moved Permanently
Response URI: {{news_url::{news}|absolute}}
---
Result: domain.tld/news/123 → domain.tld/news-reader/foobar-123.html
Result: domain.tld/news/foobar → 404 Page Not Found
  1. 带有查询字符串的重写旧URL
Type: expert
Path restriction: home.php
Request condition: context.getMethod() == 'GET' and request.query.has('page')
Response code: 301 Moved Permanently
Response URI: {{link_url::{page}|absolute}}
---
Result: domain.tld/home.php?page=123 → domain.tld/foobar-123.html
  1. 重写包含斜杠(没有查询字符串)的URL到新域名
Type: basic
Hosts restriction: [domain.com]
Path restriction: /{wildcard}
Requirements: [wildcard => .*]
Response code: 301 Moved Permanently
Response redirect URL: https://domain.tld/{wildcard}
---
Result: domain.com/blog/test → https://domain.tld/blog/test

创建自定义配置提供者

除了现有的提供者之外,您还可以创建自己的类,该类提供重写配置。新服务必须实现 Terminal42\UrlRewriteBundle\ConfigProvider\ConfigProviderInterface 接口,并使用适当的标签进行注册。

services:
    app.my_rewrite_provider:
        class: AppBundle\RewriteProvider\MyRewriteProvider
        public: false
        tags:
            - { name: terminal42_url_rewrite.provider, priority: 128 }

资源

  1. Symfony路由
  2. Symfony路由组件
  3. 如何通过条件限制路由匹配
  4. 如何定义路由要求