0

我有一个带有论坛的网站,这是我的 htaccess 文件中的当前重写规则:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*/?forum/?(.*)$ /forum.php [L]

现在,正因为如此,我有谷歌索引的网址,如:

www.domain.com/abc/forum/rest-of-url  
www.domain.com/defg/forum/rest-of-url   
www.domain.com/something-else/forum/rest-of-url 

并且所有链接都显示相同的页面,只能通过以下方式访问:

www.domain.com/forum/rest-of-url 

在 domain/ 和 /forum/ 之间没有任何词(abc、defg 等)

基本上任何类似这个链接的东西:

www.domain.com/abc/forum/rest-of-url

应该是301重定向到:

www.domain.com/forum/rest-of-url 

我尝试将重写规则更改为:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule /forum/?(.*)$ /forum.php [L]

但是后来我在谷歌网站管理员工具中为所有以前索引的 URL 收到了很多 404 错误,所以我认为可以解决 301 重定向,但我还没有弄清楚如何避免陷入循环。

任何帮助表示赞赏。谢谢

4

1 回答 1

0

通过启用 mod_rewrite 和 .htaccess httpd.conf,然后将此代码放在您.htaccessDOCUMENT_ROOT目录下:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# redirect any /foo/forum/index to /forum/index
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^/]+/(forum/[^/]+/?)$ /$1 [L,R=301,NC]

确认它工作正常后,替换R=302R=301. R=301在测试你的 mod_rewrite 规则时避免使用(永久重定向)。

于 2013-05-29T19:14:43.817 回答