使用 [QSA,L] 通过 htaccess 规则将带有尾部斜杠的 URL 重定向到不带尾部斜杠的 URL

问题描述 投票:0回答:3

最近我中断了与 WordPress 的联系,并将我网站的所有内容迁移到我自己定制的 CMS。除了一件事之外,一切都很好。我网站的博客文章之前的所有链接都有一个尾部斜杠。由于我当前的 URL 都没有尾部斜杠,因此以前的链接不再有效,我的 SEO 几乎不存在。我一直在尝试找到一个 htaccess 规则,将所有尾部斜杠 URL 重定向到不带尾部斜杠的 URL,但到目前为止,没有任何效果。

apache .htaccess http-redirect mod-rewrite trailing-slash
3个回答
4
投票

使用此重定向规则作为删除尾随斜杠的第一个规则:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [NE,R=301,L]

0
投票

您不想为了 WordPress 中的 SEO 而 100% 删除它。#

但是如果你想要的话,这会告诉你如何去做。 WordPress“/”修复。我的 WordPress 要点

可以转换,所以很有帮助 配置文件:nginx.conf ...

location  /mirror/foo/ {
        ...
        rewrite ^(.*[^/])$ $1/ 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) 

根据永久链接结构添加/删除尾部斜杠。 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$1 permanent break; rewrite ^(.*)$
http://blog.example.com$1 permanent;
确保重新加载 nginx。
使用此配置:

  1. http://www.example.com/ *重定向至
  2. http://example.comt -
  3. http://example.com/something - 其他所有内容都重定向到

0
投票

重写引擎开启

RewriteCond %{REQUEST_URI} /$ 重写规则 ^(.*)/$ /$1 [R=301,L]

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.