本文介绍了htaccess的301重定向某些URL模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想重定向: http://site.com/FOO/BAR/BAZ

http://site.com/FOO/NEW/BAZ

这样,替换它第二段在我的网址与一个新的。

so, subsitute the second segment in my URL with a new one.

最好我想使用.htaccess中和301,因为这些网页已永久性地移动。

ideally I'd like to use HTACCESS and a 301 as these pages have permanently moved.

的URL结构不会改变。理想的情况下,不会有一个查询串,但没关系过多则有

The URL structure will not change. Ideally there won't be a query string, but doesn't matter too much is there is.

感谢

8EDIT 的* - 我要澄清,说BAR不是通配符 - 它是一个字。现实生活中的例子是:

8EDIT* - I should clarify and say that "BAR" is not a wildcard - it is one word. the real life example is:

旧链接: site.com/events/details/XXX

新的链接: site.com/events/VIEW/xxx

我不想要的: site.com/events/add-new

重定向。 JUST如果第二段等于在我的情况详细信息。

to redirect. JUST if the second segment equals "details" in my case.

进一步澄清抱歉,还没有完全清楚。

further clarificationsorry haven't been exactly clear.

现有网址: site.com/events/details/%wildcard%

应该重定向到 site.com/foo/view/%wildcard%

但仅此而已。

推荐答案

这件怎么样?

RewriteEngine On
RewriteRule ^FOO/(.+)/BAZ$ FOO/NEW/BAZ [L,R=301]

编辑:不知道,但是你可能需要通过整个地址,这里有code另一个例子:

Not sure, but you may need to pass the whole address, here's another sample of code:

RewriteEngine On
RewriteRule ^FOO/(.+)/BAZ$ http://site.com/FOO/NEW/BAZ [L,R=301]

2日编辑:下面是最终的解决方案。我试了一下今天一个共享的主机,效果不错。

2nd edit: Here's the final solution. I've tried it today on a shared hosting and it worked well.

RewriteEngine On
RewriteRule ^FOO/(.+)/BAZ$ http://site.com/FOO/NEW/BAZ/ [L,R=301]

唯一不同的是,过去的例子包含斜杠( / )的新路径的末尾。

The only difference is that the last example contains a slash (/) at the end of the new path.

这篇关于htaccess的301重定向某些URL模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 12:21