将所有以单词结尾的网址重定向到没有该结尾单词的相同 URL
Redirect ALL Urls ending with a word to its same URL without that ending word
如何通过 .htaccess
重定向所有 URL,以便将所有以 -text/
结尾的 URL 重定向到 (url)/
,删除 -text
部分。
例如,所有网址(带有单词 -text/
):
https://www.example.com/example-text/
到
https://www.example.com/example/
这不完全是你的答案
但是我把这个答案放在这里可能会解决其他人的问题
function forward_to_url(){
$self = $_SERVER["REQUEST_URI"];
//The string you want to find
$itemCheck = "url";
$needle = $itemCheck. '/';
//Any Address You want redirect
$forwardlink = '/'.preg_replace('/'.strtolower($itemCheck).'/','',strtolower($self));
$length = strlen($needle);
$urlend= substr($self, -$length);
if ($urlend == $needle or $urlend == $itemCheck) {
wp_redirect($forwardlink, 301);
}
}
add_action('template_redirect', 'forward_to_url');
要删除 URL 路径末尾的 -text
(在最后一个斜杠之前),您可以在根 .htaccess
文件的顶部执行类似以下操作(在 # BEGIN WordPress
部分之前)。
例如:
RewriteRule (.+)-text/$ // [R=302,L]
不需要其他指令(因为 RewriteEngine On
指令在随后的 WordPress 代码块中)。
</code> 反向引用(在 <em> 替换 </em> 字符串中)包含在 <code>-text/
之前的 URL-路径 URL-路径.
这适用于以 -text/
结尾的 任何 URL。如果它只适用于包含单个 URL 路径段的 URLs(如您的示例所示),那么您可以修改 RewriteRule
模式 如下:
RewriteRule ^([^/]+)-text/$ // [R=302,L]
这将像以前一样匹配 /example-text/
(单个路径段),但不会匹配 /foo/bar-text
(两个路径段)。
请注意,这些是 302(临时)重定向。如果这是永久性的,则将其更改为 301,但只有在您测试它按预期工作后才可以。默认情况下,浏览器会持久缓存 301,因此可能会使测试出现问题。
如何通过 .htaccess
重定向所有 URL,以便将所有以 -text/
结尾的 URL 重定向到 (url)/
,删除 -text
部分。
例如,所有网址(带有单词 -text/
):
https://www.example.com/example-text/
到
https://www.example.com/example/
这不完全是你的答案
但是我把这个答案放在这里可能会解决其他人的问题
function forward_to_url(){ $self = $_SERVER["REQUEST_URI"]; //The string you want to find $itemCheck = "url"; $needle = $itemCheck. '/'; //Any Address You want redirect $forwardlink = '/'.preg_replace('/'.strtolower($itemCheck).'/','',strtolower($self)); $length = strlen($needle); $urlend= substr($self, -$length); if ($urlend == $needle or $urlend == $itemCheck) { wp_redirect($forwardlink, 301); } } add_action('template_redirect', 'forward_to_url');
要删除 URL 路径末尾的 -text
(在最后一个斜杠之前),您可以在根 .htaccess
文件的顶部执行类似以下操作(在 # BEGIN WordPress
部分之前)。
例如:
RewriteRule (.+)-text/$ // [R=302,L]
不需要其他指令(因为 RewriteEngine On
指令在随后的 WordPress 代码块中)。
</code> 反向引用(在 <em> 替换 </em> 字符串中)包含在 <code>-text/
之前的 URL-路径 URL-路径.
这适用于以 -text/
结尾的 任何 URL。如果它只适用于包含单个 URL 路径段的 URLs(如您的示例所示),那么您可以修改 RewriteRule
模式 如下:
RewriteRule ^([^/]+)-text/$ // [R=302,L]
这将像以前一样匹配 /example-text/
(单个路径段),但不会匹配 /foo/bar-text
(两个路径段)。
请注意,这些是 302(临时)重定向。如果这是永久性的,则将其更改为 301,但只有在您测试它按预期工作后才可以。默认情况下,浏览器会持久缓存 301,因此可能会使测试出现问题。