htaccess 重定向 301 + 重写规则冲突
htaccess redirect 301 + rewriterule conflict
我有一个旧域 (old.com),其中包含大量动态 URL 以及位于 google 中的静态 URL。我有一个新域 (new.cat),其中相同的内容具有更 seo 友好的新 url。
old.com 站点位于 IIS 主机中,因此我已将其转移到装有 Apache 的服务器上,在那里我可以使用 htaccess 将旧网址重定向到新网址。我已经用数据库中的 php 完成了它,以免手工编写每条指令。
示例动态旧网址 ==> 新网址:
old.com/mots.asp?nm=1 ==> new.cat/moix
旧.com/mots.asp?nm=2 ==> 新.cat/miol
...
示例静态旧网址 ==> 新网址:
旧.com/mesos.asp ==> 新.cat/arxiu-cronologic
old.com/mot_cerca_tema.asp?tipus=frases%20fetes ==> new.cat/tema/frases-fetes/
...
嗯,我的 .htaccess 文件有两种规则:
重定向 301 -> 对于静态旧网址(无参数)
ReweriteRule -> 用于动态旧 url(带参数)
就是这样的规则(我还有很多,为了看清楚只放了一些,"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/ [R=301,L]
问题是"redirect 301"条规则没有执行,导致404错误,因为执行的规则是最后一条。
例如:
old.com/mesos.asp results in new.cat/mesos.asp (that not exists in new.cat)
如果 "Redirect 301" 规则在重写规则之前或之后,就会出现此问题。
如果我只将重定向 301 而不是其他规则放入 htaccess 文件中,重定向将正确执行
那么,有人有任何线索可以帮助我解决这个问题吗?我想可能存在某种偏好问题,但我不明白为什么。我认为问题可能是因为 .asp 扩展名,但如果规则在隔离或使用 rewriterule 时工作正常,那么似乎不是问题。
新站点以 wordpress 作为后端,从那里我可以处理来自 old.com 的 404 错误并将它们重定向到一个特殊页面。
感谢大家。
您正在混合来自两个不同模块 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/ [R=301,L]
这里,L
停止重写引擎执行以下任何规则,这样当你请求/mesos.asp
时,它会立即重定向并且重写引擎停止,这样它就不会结束向上执行动态 url 规则。
我有一个旧域 (old.com),其中包含大量动态 URL 以及位于 google 中的静态 URL。我有一个新域 (new.cat),其中相同的内容具有更 seo 友好的新 url。
old.com 站点位于 IIS 主机中,因此我已将其转移到装有 Apache 的服务器上,在那里我可以使用 htaccess 将旧网址重定向到新网址。我已经用数据库中的 php 完成了它,以免手工编写每条指令。
示例动态旧网址 ==> 新网址:
old.com/mots.asp?nm=1 ==> new.cat/moix 旧.com/mots.asp?nm=2 ==> 新.cat/miol ...
示例静态旧网址 ==> 新网址:
旧.com/mesos.asp ==> 新.cat/arxiu-cronologic old.com/mot_cerca_tema.asp?tipus=frases%20fetes ==> new.cat/tema/frases-fetes/ ...
嗯,我的 .htaccess 文件有两种规则:
重定向 301 -> 对于静态旧网址(无参数) ReweriteRule -> 用于动态旧 url(带参数)
就是这样的规则(我还有很多,为了看清楚只放了一些,"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/ [R=301,L]
问题是"redirect 301"条规则没有执行,导致404错误,因为执行的规则是最后一条。
例如:
old.com/mesos.asp results in new.cat/mesos.asp (that not exists in new.cat)
如果 "Redirect 301" 规则在重写规则之前或之后,就会出现此问题。
如果我只将重定向 301 而不是其他规则放入 htaccess 文件中,重定向将正确执行
那么,有人有任何线索可以帮助我解决这个问题吗?我想可能存在某种偏好问题,但我不明白为什么。我认为问题可能是因为 .asp 扩展名,但如果规则在隔离或使用 rewriterule 时工作正常,那么似乎不是问题。
新站点以 wordpress 作为后端,从那里我可以处理来自 old.com 的 404 错误并将它们重定向到一个特殊页面。
感谢大家。
您正在混合来自两个不同模块 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/ [R=301,L]
这里,L
停止重写引擎执行以下任何规则,这样当你请求/mesos.asp
时,它会立即重定向并且重写引擎停止,这样它就不会结束向上执行动态 url 规则。