.htaccess redirect QUERY_STRING and not propage QUERY_STRING - apache

I would like to make a permanent redirection of
/ask?search%255Bto%255D=test
to
/question/test
I add in my .htaccess
RewriteCond %{QUERY_STRING} ^search%255Bto%255D=([a-zA-Z0-9\-]*)
RewriteRule ^ask$ /question/%1 [R=permanent,L]
With these additional lines, I get
/ask?search%255Bto%255D=test
redirect to
/question/test?search%255Bto%255D=test
My problem is than I still have the query string (?search%255Bto%255D=test) in my redirection url.
I would like to have:
/question/test
and not
/question/test?search%255Bto%255D=test
Thanks for any help

If you want to clear the query string, simply end your target URL with a question mark. Try changing your rewrite rule to:
RewriteRule ^ask$ /question/%1? [R=permanent,L]
This specifies a new, empty query string, so the old one will not be sent as part of the redirect URL.

Related

Redirect omitting parameters in .htaccess

After a couple of hours of fighting, I manage to generate an htacces file to redirect all my old URLs to the URLs of my new site (same domain). I am using the following rules to manage my subfolders redirects (my new site has only a couple of pages):
RewriteRule ^es/empresa/terminos-y-condiciones.html$ http://domain/terminos-y-condiciones.html [L]
RewriteRule ^es/servicios/registro-de-dominios/.*$ http://domain/dominios.html [R=301,NC,L]
RewriteRule ^es/empresa/.*$ http://domain/nosotros.html [R=301,NC,L]
RewriteRule ^es/component/.*$ http://domain/contacto.html [R=301,NC,L]
RewriteRule ^es(/.*)?$ / [R=301,NC,L]
The problem is that when I try to access to some pages like domain.com/empresa/testimonios-de-clientesc69a.html?lang=es it redirects me to domain/?lang=es , that is my domain+the parameter after ".html".
So, I was wondering if there was a way to eliminate this parameter and redirect only to www.domain.com ?
Thanks in advance!
To truncate a query string add a '?' sign to the end of target URLs like this:
RewriteRule ^es/empresa/.*$ http://domain/nosotros.html? [R=301,NC,L]
On Apache 2.4 or later you can use the QSD flag to discard the query string (see documentation):
When the requested URI contains a query string, and the target URI
does not, the default behavior of RewriteRule is to copy that query
string to the target URI. Using the [QSD] flag causes the query string
to be discarded.
So it would look like
RewriteRule ^es/empresa/.*$ http://domain/nosotros.html [R=301,NC,L,QSD]

rewrite apache httpd complete url with query string

I want to rewrite url from one application to another application of diferent path with query string using below code of lines in my httpd.conf
RewriteEngine On
RewriteRule ^/rforms/jsp/rform/index.jsp?(.*)$ /Project/jsps/rform/indexAIL.jsp?$1 [R]
RewriteRule ^/rforms/onlineLandingPage.do?(.*)$ /Project/onlineLandingPage.do?pid=1&loginType=2&$1 [R]
url redirects but query string param get missed.
Please suggest.
You cannot match query string in RewriteRule and there is no real need to match query string here since that will be carried over automatically.
Try these rules instead:
RewriteEngine On
RewriteRule ^/?rforms/jsp/rform/index\.jsp$ /Project/jsps/rform/indexAIL.jsp [NC,R,L]
RewriteRule ^/?rforms/onlineLandingPage\.do$ /Project/onlineLandingPage.do?pid=1&loginType=2 [R,L,NC,QSA]

apache redirection is adding query string to the new url

apache mod rewrite is acting weird on me:
im trying to redirect this:
http://www.website.com/page.php?id=34
to this:
http://www.website.com/
using this in the htaccess:
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^page\.php$ "http://www.website.com" [R=301,L]
it works fine, but it also adds the query string to the redirected url:
http://www.website.com/?id=34
how do I remove the query string from the redirected url?
By default, mod_rewrite won't change the query strings, it'll keep them unchanged and pass them to whatever the new URI is. To remove them, you need to end the new URL with a question mark. You also don't need the quotation marks around it. This should work:
RewriteRule ^page\.php$ http://www.website.com? [R=301,L]
i'm not sure if it is going to work but try:
RewriteRule ^page\.php "http://www.website.com" [R=301,L]

Redirect If URL Param Contains Specific Substring Value

I'm trying to use mod_rewrite to redirect users coming with a certain substring in their URL.
For example, INBOUND_XXX34_MPEG, if 34 is in the URL, I need to redirect to a different integer (i.e.: INBOUND_XXX20_MPEG)
Is this type fine tooth-combing possible with mod_rewrite?
EDIT what I have so far, but fails in testing:
RewriteEngine on
RewriteRule ^(.*=\w*)34(.*)$ $120$2
Solved it!
RewriteCond %{QUERY_STRING} linkid=(.*)34(_.*)$
RewriteRule (.*) $1?linkid=%120%2 [R=301,L]
This will preserve URI, additional query params, and target the substring index.
I'm not sure about what you really want but how about this:
RewriteRule ^(.*)INBOUND_XXX34_MPEG(.*)$ $1INBOUND_XXX20_MPEG$2
I do not know if the XXXis some can of variable thing?

Apache Mod_Rewrite query string to standard url

I was using a .htaccess redirect 301 to redirect a URL which then appended all the query string elements to the end, leaving me with some URLs indexed in Google as /store/product/?d=department&s=section&p=product.
I have fixed the redirect by using a RewriteRule instead which doesn't append the query strings, however I'm stuck trying to rewrite the old redirected URLs with the query strings back to the original URLs (as these are looking like two different URLs to Google now).
I have managed to get a RewriteRule to sort of work, in that /store/product/xxxxx redirects to /store/product/ as it should, it just doesn't seem to work with the whole query string of.
What I have been using is:
RewriteRule ^store/product/([a-zA-Z0-9\-_\?=&]+)$ http://www.example.com/store/product/ [NC,R=301,L]
or
RewriteRule ^store/product/\?d=department&s=section&p=product$ http://www.example.com/store/product/ [NC,R=301,L]
Hope that all makes sense!
Many thanks
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^store/product/.*$ http://www.example.com/store/product/? [NC,R=301,L]
You need to specify an empty query in your substitution to not have the original one automatically appended to the new URL:
RewriteRule ^store/product/[a-zA-Z0-9\-_=&]+$ http://www.example.com/store/product/? [NC,R=301,L]
Note the ? at the end of the substitution URL.