通过带有 [QSA,L] 的 htaccess 规则将带有尾部斜杠的 URL 重定向到没有尾部斜杠的 URL
Redirect URLs with a trailing slash to URLs with no trailing slash via htaccess rule with [QSA,L]
最近我与 WordPress 断绝了关系,并将我网站的所有内容迁移到我自己定制的 CMS。除了一件事,一切都很好。我网站博客文章的所有先前链接都有尾部斜杠。由于 none 我当前的 URL 有尾部斜线,以前的链接不再有效,我的 SEO 几乎不存在。我一直在尝试找到一个 htaccess 规则,它将所有尾部斜线 URL 重定向到没有尾部斜线的 URL,但截至目前,没有任何效果。
使用此重定向规则作为删除结尾斜线的第一条规则:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ / [NE,R=301,L]
您不想在 WordPress 中 100% 删除它以进行 SEO。#
但如果您愿意,这会告诉您如何操作。 WordPress“/”修复。我的 WordPress Gist
可以转换所以很有帮助
配置文件:nginx.conf
...
location /mirror/foo/ {
...
rewrite ^(.*[^/])$ / permanent;
...
}
...
描述 WordPress“/”PHP 修复
如果博客设置为添加尾部斜杠,则检索尾部斜杠字符串。
如果永久链接结构有尾部斜线,则有条件地添加尾部斜线,否则去除尾部斜线。该字符串通过 ‘user_trailingslashit’
过滤器传递。将从字符串中删除尾部斜杠,如果博客未设置为包含斜杠。
用法
<?php user_trailingslashit( $string, $type_of_string ); ?>
参数
$string
(string) (false) URL with or without a trailing slash.
Default: None
$type_of_url
(string) (false) The type of URL being considered (e.g. single, category, etc) for use in the filter.
Default: None
Return Value (string)
Adds/removes 基于永久链接结构的尾部斜杠。
codex.wordpress。 /Function_Reference/user_trailingslashit
2。
如果要加"/"
<?php trailingslashit( $string ) ?>
例子
<?php
$path = trailingslashit( '/home/julien/bin/dotfiles' );
?>
$path 现在将包含:
/home/julien/bin/dotfiles/
(注意结尾的斜杠)
https://codex.wordpress.org/Function_Reference/trailingslashit
3。
这是新的
任何测试中最重要的部分是断言。断言是您期望从系统获得的价值与您实际获得的价值之间的比较。最简单的测试可能只包含一个断言。示例:
public function test_trailingslashit_should_add_slash_when_none_is_present() {
$this->assertSame( 'foo/', trailingslashit( 'foo' ) );
}
assertSame()
方法接受两个参数:预期值(在本例中为硬编码字符串 'foo/')
,以及实际值(trailingslashit())
返回的值。
可以在下面找到带注释的常见断言列表。
https://make.wordpress.org/core/handbook/testing/automated-testing/writing-phpunit-tests/#assertions
试试这些规则:(in www.example.com's server block) rewrite ^/$ http://example.com permanent break; rewrite ^/main(.*)$ http://example.com permanent break; rewrite ^(.*)$
http://blog.example.com permanent;
确保重新加载 nginx。
使用此配置:
最近我与 WordPress 断绝了关系,并将我网站的所有内容迁移到我自己定制的 CMS。除了一件事,一切都很好。我网站博客文章的所有先前链接都有尾部斜杠。由于 none 我当前的 URL 有尾部斜线,以前的链接不再有效,我的 SEO 几乎不存在。我一直在尝试找到一个 htaccess 规则,它将所有尾部斜线 URL 重定向到没有尾部斜线的 URL,但截至目前,没有任何效果。
使用此重定向规则作为删除结尾斜线的第一条规则:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ / [NE,R=301,L]
您不想在 WordPress 中 100% 删除它以进行 SEO。#
但如果您愿意,这会告诉您如何操作。 WordPress“/”修复。我的 WordPress Gist
可以转换所以很有帮助 配置文件:nginx.conf ...
location /mirror/foo/ {
...
rewrite ^(.*[^/])$ / permanent;
...
}
...
描述 WordPress“/”PHP 修复 如果博客设置为添加尾部斜杠,则检索尾部斜杠字符串。
如果永久链接结构有尾部斜线,则有条件地添加尾部斜线,否则去除尾部斜线。该字符串通过 ‘user_trailingslashit’
过滤器传递。将从字符串中删除尾部斜杠,如果博客未设置为包含斜杠。
用法
<?php user_trailingslashit( $string, $type_of_string ); ?>
参数
$string
(string) (false) URL with or without a trailing slash.
Default: None
$type_of_url
(string) (false) The type of URL being considered (e.g. single, category, etc) for use in the filter.
Default: None
Return Value (string)
Adds/removes 基于永久链接结构的尾部斜杠。 codex.wordpress。 /Function_Reference/user_trailingslashit
2。 如果要加"/"
<?php trailingslashit( $string ) ?>
例子
<?php
$path = trailingslashit( '/home/julien/bin/dotfiles' );
?>
$path 现在将包含:
/home/julien/bin/dotfiles/
(注意结尾的斜杠) https://codex.wordpress.org/Function_Reference/trailingslashit
3。 这是新的 任何测试中最重要的部分是断言。断言是您期望从系统获得的价值与您实际获得的价值之间的比较。最简单的测试可能只包含一个断言。示例:
public function test_trailingslashit_should_add_slash_when_none_is_present() {
$this->assertSame( 'foo/', trailingslashit( 'foo' ) );
}
assertSame()
方法接受两个参数:预期值(在本例中为硬编码字符串 'foo/')
,以及实际值(trailingslashit())
返回的值。
可以在下面找到带注释的常见断言列表。 https://make.wordpress.org/core/handbook/testing/automated-testing/writing-phpunit-tests/#assertions
试试这些规则:(in www.example.com's server block) rewrite ^/$ http://example.com permanent break; rewrite ^/main(.*)$ http://example.com permanent break; rewrite ^(.*)$
http://blog.example.com permanent;
确保重新加载 nginx。
使用此配置: