0

I have an old domain (old.com) with plenty of dynamic urls and also static urls that are well positioned in google. I have a new domain (new.cat) where the same content has new urls more seo friendly.

The old.com site were in a IIS host, so I have transfered it to a server with apache, where I could use htaccess to redirect the old urls to the new ones. I have done it with php from the database in order to not to have writing every instruction by hand.

Examples dynamic old urls ==> new urls:

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

Examples 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/ ...

Well, my .htaccess file has two kinds of rules:

Redirect 301 -> for static old urls (without parameters) ReweriteRule -> for dynamic old urls (with parameters)

These are the kind of rules (I have a lot more of them, but I only put someones in order to be clear. The "new.cat" is really filled with the httpp:// protocol)

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]

The problem is that the "redirect 301" rules are not executing, resulting in a 404 error because de rule executed is the last one.

For example:

old.com/mesos.asp results in new.cat/mesos.asp (that not exists in new.cat)
  • This problem occurs either if the "Redirect 301" rules are before the rewrite rules or after them.

  • If I put only the redirects 301 and not the others rules in the htaccess file, the redirects are executed correctly

So, anyone have any clue that could help me to resolve this problem? I suppose that there is some kind of preference problem, but I can't see why. I thought the problem could be because the .asp extension of urls, but if the rules work fine isolated or with rewriterule, it seems not a problem then.

The new site has wordpress as backend, and from there I can deal with 404 errors coming from the old.com and redirect them to an special page.

Thanks to all.

nuriarai
  • 15
  • 6

1 Answers1

1

You're mixing directives from two different modules, mod_rewrite and mod_alias. Since both modules get applied to the same request, and neither modules care what the other is doing, you can have both modules redirect the same request. In order to prevent that, you need to just use 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]

Here, the L stops the rewrite engine from executing any of the following rules so that when you request /mesos.asp, it redirects immediately and the rewrite engine stops so that it won't end up executing the dynamic url rule.

Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • Thanks for your answer, now its working with your suggestions. Then, I understand that is not possible to have in the same htaccces file directives from differents modules, as I was doing before, even though the redirections have not conflicts between them. Isn't it? – nuriarai May 26 '15 at 23:12
  • @nuriarai the redirections *do* have conflicts. "mesos.asp" matches the `Redirect` rule as well as the `RewriteRule ^(.*)$` rule. So the same request ends up getting modified by both mod_rewrite and mod_alias. – Jon Lin May 27 '15 at 00:04
  • Thanks @Jon Lin! now I understand and with your clues I also have found a similar question than mine that haven't found before, it has a link to an old but well explained post about the mod_alias and mod_rewrite tricks. The question: http://stackoverflow.com/questions/4856193/using-mod-rewrite-and-mod-alias-redirect-301-together-in-htaccess. Thank you! – nuriarai May 27 '15 at 07:42