301重定向到完整路径
问题描述:
我有很多不正确的反向链接,这些链接指向:
I have a lot incorrect back links, the links are pointing to:
http://www.domain.com/tags/keyword
而正确的路径是
http://www.domain.com/tags/keyword/
有数百个......我怎么能 301 从错误的链接重定向到正确的链接?
there are hundreds of those...how could i 301 redirect from the wrong links to the correct links?
在此先感谢您
答
你可以试试这个代码:
RewriteBase /
RewriteRule ^tags/([^/]+)$ /tags/$1/ [L,R=301]
-
RewriteBase/
告诉 apache 您的 URI 以/
开头.如果您的站点位于子文件夹中,则应改为编写RewriteBase/subfolder/
. -
^tags/([^/]+)$
:搜索以tags/
开头的 URI,后跟[^/]+
表示除/
之外的任何字符.它周围的( )
用于捕获它并在重定向中使用它.因此,我们捕获 URI 中tags/.../
之间不是/
的任何字符.(^
标记字符串的开始,$
标记字符串的结尾) -
/tags/$1/
是重定向.$1
表示第一个捕获的元素(那个巫婆在( )
之间). -
[L,R=301]
向 apache 指示它应该停止处理其他规则并使用 301 标头代码重定向. -
RewriteBase /
tells apache that your URI starts by a/
. If your site was in a subfolder you should writeRewriteBase /subfolder/
instead. -
^tags/([^/]+)$
: you search for an URI starting withtags/
followed by[^/]+
that means any characters except/
. The( )
around it are there to capture it and use it in the redirection. So we capture any characters that are not/
betweentags/.../
in the URI. (^
marks the start of the string while$
marks the end) -
/tags/$1/
is the redirection.$1
means the first previous captured element (the one witch was between( )
). -
[L,R=301]
indicates to apache that it should stop process other rules and redirect with a 301 header code.