Redirecting 301 using htaccess

3

I have a problem with some redirections in a site when creating a redirection of a <etiqueta>.html to a <categoria>/ . The problem is that you drag the variable.

The <etiqueta> s are a predefined list by me (it's not any text).


Example 1

  • Original URL in the request:

    dominio.com/videos/coches-electricos.html
    
  • URL to which you must redirect:

    dominio.com/gratis/coches-electricos/
    
  • And you must rewrite it to access the resource (eliminating all the GET parameters that you might have) to:

    dominio.com/index.php?cat=coches-electricos
    


Example 2

and I want it to be:

https://www.dominio.com/gratis/coches-electricos/

redirected to:

https://www.dominio.com/categoria/coches-electricos/?tag=coche-electrico


Code

The htaccess code to create the rewrite mod I'm using is:

Rewriterule ^videos/(.*)_(.*).html$ index.php?tag=$1&page=$2 [L,NC]
Rewriterule ^videos/(.*).html$ index.php?tag=$1 [L,NC]
Rewriterule ^gratis-(.*).html$ index.php?page=$1 [L,NC]
Rewriterule ^gratis/(.*)/(.*)/$ index.php?cat=$1&page=$2 [L,NC]
Rewriterule ^gratis/(.*)/$ index.php?cat=$1 [L,NC]

and to create the 301 redirect I'm using:

redirect 301 /videos/coches-electricos.html https://www.dominio.com/gratis/coches-electricos/
    
asked by vpme 02.07.2017 в 21:20
source

1 answer

3

You can put all the redirections at the beginning:

Redirect 302 /videos/coches-electricos.html /gratis/coches-electricos/
Redirect 302 /videos/bici.html              /gratis/bici/
Redirect 302 /videos/mecano.html            /gratis/mecano/
  • Notice that I'm using a 302 redirect. The subject is like this: never use a 301 to try something because any error can be cached (from one of the nodes or from your browser) . Always with 302. Once you are more than completely sure that it works as you want and that you will not modify it ever in the future, then you can change it to 301 instead.

The rest of the rewriting rules would continue to apply. I took the liberty to group them a little.

RewriteEngine on

# REDIRECCiÓN
# (probalo con 302, si funciona se cambia a 301 -nunca jamás uses un 301 hasta estar convencido)
# Redirecciona un listado de posibles tags (descarta parámetros GET)
Redirect 302 /videos/coches-electricos.html /gratis/coches-electricos/?
Redirect 302 /videos/bici.html              /gratis/bici/?
Redirect 302 /videos/mecano.html            /gratis/mecano/?


# esta es otra forma de redireccionar a 1 solo
# RewriteRule ^videos/(coches-electricos)\.html$ gratis/$1/ [R=302,NC,QSD]

# o si se prefiere, a todos juntos:
# RewriteRule ^videos/(coches-electricos|bici|mecano)\.html$ gratis/$1/ [R=302,NC,QSD]


# REESCRITURA
Rewriterule ^videos/([^_/]*)(?:_([^/]*))?\.html$ index.php?tag=$1&page=$2 [L,NC]
Rewriterule ^gratis(?:/([^/]+)(?:/([^/]+))?/?|-([^/]+)\.html)$ index.php?cat=$1&page=$2$3 [L,NC]


I uploaded a demo to a free hosting with this same .htaccess. You can try:

  • link (redirect)
  • link (this is in the list of redirects)
  • link
  • link
  • link
  • link
  • link
  • answered by 02.07.2017 в 22:37