301永久重定向各种深度

时间:2014-11-23 00:09:47

标签: .htaccess redirect

我将此代码用于R301的1类深度。例如,我想将http://sitesample.ltd/new/mynews重定向到http://sitesample.ltd/new/这是我的代码:

RewriteRule ^mynews/(.+)$ http://sitesample.ltd/new/$1 [R=301,L]

我想从网址中删除深度2和3。例如,将http://sitesample.ltd/new/mynews/photo重定向到http://sitesample.ltd/new/。我试试这个但不行:

RewriteRule ^mynews/(.+)/?$ http://sitesample.ltd/new/$2 [R=301,L]
RewriteRule ^mynews/(.+)/(.+)/?$ http://sitesample.ltd/new/$3 [R=301,L]

另一次尝试但没有运气:

RewriteRule ^mynews/([^/]+)/?$ http://sitesample.ltd/new/$2 [R=301,L]
RewriteRule ^mynews/(.+)/([^/]+)/?$ http://sitesample.ltd/new/$3 [R=301,L]

1 个答案:

答案 0 :(得分:2)

如果您尝试这样做:

mynews/xxxx -> http://sitesample.ltd/new/xxxx
mynews/yyyy/xxxx -> http://sitesample.ltd/new/xxxx
mynews/zzzz/yyyy/xxxx -> http://sitesample.ltd/new/xxxx

你可以这样做:

RewriteRule ^mynews/([^/]+)/?$ http://sitesample.ltd/new/$1 [R=301,L]
RewriteRule ^mynews/[^/]+/([^/]+)/?$ http://sitesample.ltd/new/$1 [R=301,L]
RewriteRule ^mynews/[^/]+/[^/]+/([^/]+)/?$ http://sitesample.ltd/new/$1 [R=301,L]

或者只有这个:

RewriteRule ^mynews/(?:[^/]+/){0,2}([^/]+)/?$ http://sitesample.ltd/new/$1 [R=301,L]