palmtree / canonical-url-bundle
规范URL Symfony组件
v1.1.0
2019-08-14 17:22 UTC
Requires
- php: >=5.6
- symfony/config: ~2.7|~3.0
- symfony/dependency-injection: ~2.7|~3.0
- symfony/http-kernel: ~2.7|~3.0
- symfony/routing: ~2.7|~3.0
- twig/twig: ~1.28|~2.0
Requires (Dev)
- palmtree/php-cs-fixer-config: ^1.0
- phpunit/phpunit: ~8.3
- symfony/yaml: ~4.3
README
CanonicalUrlBundle
是一个用于将多个相同资源的URL重定向到单个规范URL的Symfony组件。
例如,如果你有一个名为 /about-us 的资源,它位于网站 example.org 上,它可以被以下方式访问
http://example.org/about-us
http://example.org/about-us/
http://www.example.org/about-us
http://www.example.org/about-us/
https://example.org/about-us
https://example.org/about-us/
https://www.example.org/about-us
https://www.example.org/about-us/
当用户使用上述任何URL请求资源时,CanonicalUrlBundle
将根据预定义的网站URL构建一个规范URL,如果请求的URL不匹配,则执行HTTP重定向。
该组件还可以向你的twig模板中添加一个 <link rel="canonical">
标签,有关如何操作的说明,请参阅使用说明部分。
安装
步骤 1:下载组件
打开命令行,进入你的项目目录,执行以下命令以下载此组件的最新稳定版本
composer require palmtree/canonical-url-bundle
此命令需要你已全局安装Composer,如Composer文档中的安装章节中所述。
步骤 2:启用组件
然后,通过将其添加到项目 app/AppKernel.php
文件中注册的组件列表来启用组件
<?php // app/AppKernel.php // ... class AppKernel extends Kernel { public function registerBundles() { $bundles = array( // ... new Palmtree\CanonicalUrlBundle\PalmtreeCanonicalUrlBundle(), ); // ... } // ... }
步骤 3:配置组件
将组件的配置添加到 app/config/config.yml
palmtree_canonical_url: site_url: 'https://example.org' # replace with your full site URL (without trailing slash) redirect: true # Set to false disable redirects if you just want to use the canonical link tag redirect_code: 301 # Leave this at 301 for SEO trailing_slash: false # Set to true if your routes and canonical URLs contain a trailing slash
使用说明
要向你的页面添加 <link rel="canonical">
标签,请在twig模板的 <head>
中包含以下代码
{{ palmtree_canonical_link_tag() }}
href属性默认为当前请求的规范URL,但可以被覆盖
{{ palmtree_canonical_link_tag('http://example.org/my-custom-link') }}
许可证
此组件以 MIT许可证 发布