1

I have a problem with .htaccess rules:

I want to redirect this url:

/productos/ficha/carretillas-termicas/dx.html To this one:

/productos/ficha/carretillas-termicas/grendia-es-serie-fd20-35n3.html

I'm using this .htaccess rule:

Redirect 301 /productos/ficha/carretillas-termicas/dx.html /productos/ficha/carretillas-termicas/grendia-es-serie-fd20-35n3.html

But the server redirects to:

/productos/ficha/carretillas-termicas/grendia-es-serie-fd20-35n3.html?catalogo=carretillas-termicas&slug=gx

Notice the arregation of params "?catalogo=carretillas-termicas&slug=gx"

Both URL's are the same:

/productos/ficha/carretillas-termicas/grendia-es-serie-fd20-35n3.html?catalogo=carretillas-termicas&slug=gx
/productos/ficha/carretillas-termicas/grendia-es-serie-fd20-35n3.html

So, I want to delete the params to avoid two different URL's with the same content (a SEO problem)

The .htaccess rule that generate the "productos" URL's is this:

# Converts urls like:
# productos_ficha.php?catalogo=XXX&slug=YYY
# to
# productos/ficha/XXX/YYY.html

RewriteRule ^(.*)/(.*)/(.*)/(.*).html$ $1_$2.php?catalogo=$3&slug=$4 [L]


I tried some of the solutions I found in this site, but they does not works for my case. Thank you a lot.

MrWhite
  • 43,179
  • 8
  • 60
  • 84

1 Answers1

0

(From the URL and rule you've posted you should be seeing slug=dx, not slug=gx?)

You have a conflict between the mod_alias Redirect directive and the mod_rewrite RewriteRule. mod_rewrite is processed before mod_alias (despite the apparent order of the directives) and rewrites the request, appending the query string. The Redirect then issues a redirect on the original URL-path, but appends the rewritten query string.

Instead of using mod_alias Redirect directive, you need to use a mod_rewrite RewriteRule instead to perform the redirect. And this must go _before_mthe existing rewrite.

For example:

RewriteRule ^(productos/ficha/carretillas-termicas)/dx\.html$ /$1/grendia-es-serie-fd20-35n3.html [R=301,L]

# Converts urls like:
# productos_ficha.php?catalogo=XXX&slug=YYY
# to
# productos/ficha/XXX/YYY.html

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)\.html$ $1_$2.php?catalogo=$3&slug=$4 [L]

To save repetition I captured the URL-path in the first rule (redirect) and used the corresponding backreference $1 in the substitition string.

I also modified the regex in the second rule (rewrite) since it was far to general and very inefficient (requiring a lot of backtracking).

MrWhite
  • 43,179
  • 8
  • 60
  • 84