[英].htaccess redirect for specific URL structures
I have the following URL: 我有以下网址:
https://www.site-a.xyz/tutorials/post-name/2
I need it to redirect to the following URL 我需要它重定向到以下URL
https://www.site-b.xyz/post-name/2
Essentially If there is a trailing number element to the URL (in this case /2
) I need the /tutorials/
part of the URL to be removed. 本质上,如果URL后面有数字元素(在本例中为
/2
),则需要删除URL的/tutorials/
部分。
Note: ONLY if there is a trailing number 注意:仅当有尾数时
Try the following (using mod_rewrite) near the top of your .htaccess file at www.site-a.xyz
: 在
www.site-a.xyz
的.htaccess文件顶部附近尝试以下操作(使用mod_rewrite):
RewriteEngine On
RewriteRule ^tutorials/([^/]+/\d+)$ https://www.site-b.xyz/$1 [R=302,L]
In this case, the trailing "number" can be 1 or more digits. 在这种情况下,结尾的“数字”可以是1个或多个数字。 If it is only a single digit (as in your example) then this should be simplified (change
\\d+
to \\d
). 如果只有一个数字(如您的示例),则应简化(将
\\d+
更改为\\d
)。 The $1
is a backreference to the captured group in the RewriteRule
pattern . $1
是RewriteRule
模式中对捕获组的反向引用。
Note that this is a 302
(temporary) redirect, if this is intended to be permanent then change to 301
when you are sure it's working OK. 请注意,这是
302
(临时)重定向,如果要永久301
,请在确定运行正常时更改为301
。 301s are cached by the browser so can make testing problematic. 301由浏览器缓存,因此可能会使测试产生问题。
UPDATE: To allow for an optional trailing slash on the source URL then add /?
更新:要允许在源URL上使用可选的尾部斜杠,请添加
/?
near the end of the RewriteRule
pattern, like so: 在
RewriteRule
模式的末尾附近,如下所示:
RewriteRule ^tutorials/([^/]+/\d+)/?$ https://www.site-b.xyz/$1 [R=302,L]
This notatably strips that optional trailing slash from the redirect target. 值得注意的是,这会从重定向目标中删除该可选的尾部斜杠。 (Thus avoiding any duplicate content issues.)
(从而避免出现任何重复的内容问题。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.