Redirect multiple requests get in a single line or function

3

I have finished a website and I am creating the necessary 301 redirects in the file .htaccess .

I have one of the old web sitemaps that has 100+ requests of the type:

?menu=nombre-del-plato

On the new website there are no similar requests and all should be redirected to, say, http://mirestaurante.com/menu

Is there a way to avoid these 100+ equal lines in .httacces ?:

Instead of:

Redirect 301 /?menu=nombre-del-plato1 http://mirestaurante.com/menu
Redirect 301 /?menu=nombre-del-plato2 http://mirestaurante.com/menu
Redirect 301 /?menu=nombre-del-plato3 http://mirestaurante.com/menu
Redirect 301 /?menu=nombre-del-plato4 http://mirestaurante.com/menu

Make a line or function similar to:

Redirect 301 ?menu* http://mirestaurante.com/menu

EDIT: These requests are in two languages

Redirect 301 /?menu=nombre-del-plato3 http://mirestaurante.com/menu
Redirect 301 /ca/?menu=nombre-del-plato3 http://mirestaurante.com/ca/menu
    
asked by Jordi Castilla 10.05.2016 в 12:45
source

1 answer

3

It seems that all the URLs you want to redirect have one thing in common: they are all of type ?menu=nombre-del-plato , so in the query string there is the string ?menu= . So, what you could do is create a single rule in .htaccess that redirects if that string is found.

The rule would look like this:

# Si la solicitud incluye un query string con el menú, entonces redirecciona al menú
RewriteCond %{QUERY_STRING} menu=
RewriteRule . http://mirestaurante.com/menu? [L,R=301]

This redirects permanently (by the flag R = 301) all the URLs containing ?menu= in the query string to the URL http://mirestaurante.com/ , eliminating the parameters of the URL (for the ? at the end of the URL to which it is redirected), and it is the last rule that is reviewed (by the L flag).

Now, if you want to redirect differently depending on the language, the simplest thing would be to have two rules (although I'm sure they could be combined into one):

# Si la solicitud incluye un query string con el menú y la cadena /ca/ en la URI
RewriteCond %{QUERY_STRING} menu=
RewriteCond %{REQUEST_URI} /ca/
# Entonces redirecciona al menú en catalán
RewriteRule . http://mirestaurante.com/ca/menu? [L,R=301]

# Si la solicitud incluye un query string con el menú, entonces redirecciona al menú
RewriteCond %{QUERY_STRING} menu=
RewriteRule . http://mirestaurante.com/menu? [L,R=301]

It is important that you put the most specific rule first, because if you put the most generic one before, then the second rule will never be reached.

    
answered by 10.05.2016 / 13:34
source