rewrite apache httpd complete url with query string - apache

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]

Related

apache query string rewrite rules

I am setting up Query string redirect :
expo.com/en/general/campaigns/on-second-thought.html?slide=ost-2016-tank to
expo.com/en/general/campaigns/on-second-thought/ost-2016-tank.html
RewriteCond %{HTTP_HOST} ^(.*)expo\.com
RewriteCond %{QUERY_STRING} slide=ost-2016-tank
RewriteRule  ^/en/general/campaigns/on-second-thought.html?$  http://www.expo.com/en/general/campaigns/on-second-thought/ost-2016-tank.html [R=301,L,NC]
redirect happening but its appending ?slide=ost-2016-tank like below
http://www.expo.com/en/general/campaigns/on-second-thought/ost-2016-tank.html?slide=ost-2016-tank
slide=ost-2016-tank parameter is added to redirected page
Since your rule does not define a new query string, the default behavior of Apache is to copy the old query string to the new URL. To get rid of it, append a ? to the address you rewrite/redirect to:
RewriteRule ^/en/general/campaigns/on-second-thought\.html?$ http://www.expo.com/en/general/campaigns/on-second-thought/ost-2016-tank.html? [R=301,L,NC]
Or, for Apache >= 2.4, you can also use the flag QSD (Query String Discard):
RewriteRule ^/en/general/campaigns/on-second-thought\.html?$ http://www.expo.com/en/general/campaigns/on-second-thought/ost-2016-tank.html [R=301,L,NC,QSD]
Simply add a blank query string when redirecting:
RewriteCond %{HTTP_HOST} ^(.*)expo\.com
RewriteCond %{QUERY_STRING} ^slide=(ost-2016-tank)$
RewriteRule ^(/?en/general/campaigns/on-second-thought)\.(html)$ $1/%1.$2? [R=301,L,NC]
No need to mention http://expo.com again when redirecting. It'll automatically redirect to the same hostname because of R flag. No need to repeat same strings over and over. Using match groups and referencing them later works.
Your pattern had .html?$ in it, which actually means that it'll match .html as well as .htm. You do not receive query strings in RewriteRule context.

Apache rewrite - match query string

I understand that using rewrites in apache if i want to match the query string then i should use
RewriteCond %{QUERY_STRING}
Which is fine, however, I have a csv with around 2000 urls to rewrite, a lot contain random query strings. It will be a painstaking process to go through each and create the rule.
Is there any generic way to have the rewrite look at the entire url, including the query string and redirect it?
try rewritemap
example config for vurtual host
RewriteEngine on
RewriteMap mymap "txt:/path/to/map.txt"
RewriteCond %{QUERY_STRING} ^(.+)$
RewriteCond ${mymap:%1} >""
RewriteRule ^(.*)$ ${mymap:%1} [L]
and map.txt example
p=1&i=1 /test.php?n=2
p=1&i=2 /test.php?n=4
p=1&i=3 /test.php?n=6
...
Suppose that it is suitable for site with no hign load :)

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]

.htaccess redirect QUERY_STRING and not propage QUERY_STRING

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.

How to do a htaccess rewrite with a question mark ? in the original url

I've got to redirect a URL that has a question mark in it, and I just can't figure out the proper .htaccess code to make it happen.
Example:
redirect 301 /home.php?cat=16 http://www.example.com/furniture.html
I believe I need to do a rewrite condition, but I'm no htaccess expert.
With the Redirect directive from mod_alias you can only examine the URI path and not the query (applies to all directives of mod_alias). If you want to examine the URI query you need to use mod_rewrite:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^cat=16$
RewriteRule ^home\.php$ http://www.example.com/furniture.html? [L,R=301]
The empty query in the replacement will prevent that the original query is being appended to the new URI.
From the apache2 mod_rewrite docs:
What is matched?
The Pattern will initially be matched
against the part of the URL after the
hostname and port, and before the
query string. If you wish to match
against the hostname, port, or query
string, use a RewriteCond with the
%{HTTP_HOST}, %{SERVER_PORT}, or
%{QUERY_STRING} variables
respectively.
for more complex parameters (like site.com/index.php?blaa=1&id=33 )
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} blaa=1&id=33
RewriteRule ^(.*)$ http://www.site.com/yourpage.html? [R=301,L]
</IfModule>