首页 文章

htaccess重定向301重写冲突

提问于
浏览
0

我有一个旧的域名(old.com),有很多动态网址和静态网址,这些网址在谷歌中位置很好 . 我有一个新的域名(new.cat),其中相同的内容有新的URL更友好 .

old.com站点位于IIS主机中,因此我将其转移到带有apache的服务器,在那里我可以使用htaccess将旧URL重定向到新的URL . 我已经用数据库中的php完成了它,以便不用手写每条指令 .

示例动态旧网址==>新网址:

old.com/mots.asp?nm=1 ==> new.cat/moix old.com/mots.asp?nm=2 ==> new.cat/miol ...

示例static old urls ==> new urls:

old.com/mesos.asp ==> new.cat/arxiu-cronologic old.com/mot_cerca_tema.asp?tipus=frases%20fetes ==> new.cat/tema/frases-fetes/ ...

好吧,我的.htaccess文件有两种规则:

重定向301 - >用于静态旧网址(不带参数)ReweriteRule - >用于动态旧网址(带参数)

这些是规则(我有更多的规则,但我只是为了清楚而放了一些 . “new.cat”真的充满了httpp://协议)

Redirect 301 /mesos.asp new.cat/mots/arxiu-cronologic/
Redirect 301 /inici.asp new.cat/
Redirect 301 /mot_cerca.asp new.cat/mots/arxiu-cronologic/

RewriteEngine on
RewriteCond %{QUERY_STRING} ^nm=1$
RewriteRule ^mot.asp$ new.cat/moix/ [R=301,L]
RewriteCond %{QUERY_STRING} ^nm=2$
RewriteRule ^mot.asp$ new.cat/miol/ [R=301,L]
RewriteRule ^mot_cerca_resultat.asp$ new.cat/miol/ [R=301,L]
RewriteCond %{QUERY_STRING} ^nm=3$
RewriteRule ^mot.asp$ new.cat/gat-vell/ [R=301,L]
RewriteRule ^mot_cerca_resultat.asp$ new.cat/gat-vell/ [R=301,L]

#last rule for all the other dynamic urls
RewriteRule ^(.*)$ new.cat/$1 [R=301,L]

问题是“重定向301”规则没有执行,导致404错误,因为执行的de rule是最后一个 .

例如:

old.com/mesos.asp results in new.cat/mesos.asp (that not exists in new.cat)
  • 如果“重定向301”规则在重写规则之前或之后,则会出现此问题 .

  • 如果我只将重定向301而不是其他规则放在htaccess文件中,则重定向会正确执行

那么,任何人都有任何线索可以帮助我解决这个问题?我认为存在某种偏好问题,但我不明白为什么 . 我认为问题可能是因为网址的.asp扩展,但如果规则工作正常孤立或使用rewriterule,那么它似乎不是问题 .

新网站有wordpress作为后端,从那里我可以处理来自old.com的404错误并将它们重定向到特殊页面 .

谢谢大家 .

1 回答

  • 1

    您正在混合来自两个不同模块mod_rewrite和mod_alias的指令 . 由于两个模块都应用于同一个请求,并且两个模块都不关心另一个模块正在做什么,因此您可以让两个模块重定向同一个请求 . 为了防止这种情况,你只需要使用mod_rewrite:

    RewriteEngine on
    
    RewriteRule ^mesos.asp new.cat/mots/arxiu-cronologic/ [L,R=301]
    RewriteRule ^inici.asp new.cat/ [L,R=301]
    RewriteRule ^mot_cerca.asp new.cat/mots/arxiu-cronologic/ [L,R=301]
    
    RewriteCond %{QUERY_STRING} ^nm=1$
    RewriteRule ^mot.asp$ new.cat/moix/ [R=301,L]
    RewriteCond %{QUERY_STRING} ^nm=2$
    RewriteRule ^mot.asp$ new.cat/miol/ [R=301,L]
    RewriteRule ^mot_cerca_resultat.asp$ new.cat/miol/ [R=301,L]
    RewriteCond %{QUERY_STRING} ^nm=3$
    RewriteRule ^mot.asp$ new.cat/gat-vell/ [R=301,L]
    RewriteRule ^mot_cerca_resultat.asp$ new.cat/gat-vell/ [R=301,L]
    
    #last rule for all the other dynamic urls
    RewriteRule ^(.*)$ new.cat/$1 [R=301,L]
    

    这里, L 阻止重写引擎执行以下任何规则,这样当你请求 /mesos.asp 时,它立即重定向,重写引擎停止,这样它就不会最终执行动态URL规则 .

相关问题