如何通过 htaccess 进行永久重定向?

时间:2021-04-11 12:18:56

标签: .htaccess redirect

Google 已将一些带有空值的 URL 编入索引 - 空格 - 在每个 URL 的末尾,我想通过 .htaccess 进行永久重定向,这是我的代码,但它没有重定向:

RewriteRule ^/profile/userx/([a-zA-Z])+/[a-zA-Z]+/[a-zA-Z]/'%20'$ https://mywebsite.net/profile/userx/([a-zA-Z]+)/[a-zA-Z]+/[a-zA-Z]/ [R=301,L]

如何说任何末尾有空格的网址都将其重定向到没有空格的网址?

1 个答案:

答案 0 :(得分:1)

您可以使用此规则作为从 URL 末尾删除 1+ 个空格的最顶层规则:

RewriteEngine On

RewriteRule ^(.*)\s+$ /$1 [L,NE,R=301]

# other rules go below this line

\s+$ 匹配 URI 末尾的 1+ 个空格,.* 匹配空格之前的任何内容。

相关问题