apache rewrite rule with query parameters - apache

I need to redirect a existing url with three dynamic parameters values.
My existing url is like :
http://localmach.test.it:90/aa/nonLoggedUser.portal?appb=true&event=AppEvent&Code=1.564&mobile=789754654&locale=en
Now I want to redirect it to different ip with same parameters like :
http://17.22.11:90/aa/mycare?appb=true&event=AppEvent&Code=1.564&mobile=789754654&locale=en
I'm using apache url redirect in httpd.conf file.
I'm able to redirect url without parameters, but fail to redirect with parameters present.
I have tried below rewrite in httpd.conf but its not working
<VirtualHost *:90>
ServerName localmach.test.it
RewriteEngine on
RewriteCond %{QUERY_STRING} ^Code=([0-9]*)&mobile=([0-9]*)&locale=([a-z]*)$
RewriteRule ^/(.*)(aa/nonLoggedUser.portal?appb=true&event=AppEvent&)(.*)$ http://17.22.11:90/aa/mycare?appb=true&event=AppEvent&Code=%1&mobile=%2&locale=%3$
</VirtualHost>
Please help
Thanks in advance.

Parameter Code is not the first one in query string so ^ before it won't match
You cannot match query string in RewriteRule pattern.
Also regex for matching query parameters is also not correct.
You cannot use regex in target URI so $ is treated a literal $ there.
You can use this refactored rule:
RewriteEngine on
RewriteCond %{QUERY_STRING} (?:^|&)(Code=[^&]*&mobile=[^&]*&locale=[^&]*)(?:&|$) [NC]
RewriteRule ^/?aa/nonLoggedUser\.portal$ http://17.22.11:90/aa/mycare?appb=true&event=AppEvent&%1 [L,NC,NE,R=301]

Related

Permanent Redirect of a variable URL

I'm having troubles with my .htaccess permanent redirection code which is not working:
RedirectPermanent ^shop/?attro-attributes=([0-9]*) http://www.second-website.example
I would like to redirect URLs from first-website:
first-website.example/shop/?attro-attributes=01
first-website.example/shop/?attro-attributes=02
first-website.example/shop/?attro-attributes=...
first-website.example/shop/?attro-attributes=9999
to second-website URL: http://www.second-website.example/
RedirectPermanent and other redirect directives from mod_alias have no access to the query string. Your match pattern can't contain the ? from the URL or match on anything after it.
You'll instead have to use mod_rewrite which can access the query string via RewriteCond.
RewriteEngine on
RewriteCond %{QUERY_STRING} attro-attributes=[0-9]*
RewriteRule ^/?shop/$ https://www.second-website.example/ [L,R=301]

Redirect URL based on condition Apache Httpd.conf

I am using Apache web server "httpd-2.4.25-win64-VC14" which is integrated with JBoss. The Port redirect is working properly. Now i want to replace URL based on some condition let's say if URL contains 'Mobile' then i want to replace it with 'Mobile/web' and forward it.
For this I am using <Directory> tag enclosed within <VirtualHost> tag. Now most the online references I am finding are having URL's in front of tag such as 'var/www/example' but I want to redirect based on localhost as I am running Jboss locallly.
So how should I write the tag contents , i tried with the below
<VirtualHost *:80>
<Directory /var/www/example/>
Allow From All
RewriteEngine On
RewriteCond %{QUERY_STRING} (manage)
RewriteRule ^Mobile http://%{HTTP_HOST}/Mobile/web=%1 [NC,L]
</Directory>
</VirtualHost>
Like http://localhost:8081/Mobile/register should be replaced with http://localhost:8081/Mobile/web/register
Please Suggest
It looks like you want to redirect only when the Query String contains a word "manage":
RewriteCond %{QUERY_STRING} (manage)
If you want to redirect when the Query String doesn't contain a certain word you can use a pattern like this:
RewriteCond %{QUERY_STRING} (!manage)
And the rewrite rule like this one:
RewriteRule ^Mobile /Mobile/web/register [NC,L,QSA]
QSA may be helpful here:
Appends any query string from the original request URL to any query
string created in the rewrite target.
This solution will pass all of the Query String parameters and redirect from http://localhost:8081/Mobile/register?arg1=value1&arg2=value2 to http://localhost:8081/Mobile/web/register?arg1=value1&arg2=value2.

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]

I Cannot get rewriterule to work

I'm using VertrigoServ 2.27 on my laptop (localhost:8080).
Rewrite module is enabled and i tested it with alice.html and bob.html example
(http://stackoverflow.com/questions/6944521/url-rewriting-on-localhost)
and it works with .htacces inside www-subfolder. And I also put rubbish text inside .htaccess and
I got error from Apcheserver so rewrite mod is running and I cab use rules.
here is the rule: (inside /www/folder1/.htaccess)
Options +FollowSymLinks
RewriteEngine On
RewriteRule /(.*) /index.php?disp=$1 [QSA,L]
So when I put this url into browser my index page loaded ok.
http://localhost:8080/folder1/index.php
And the problem is here: When I request login via index.page(login page) and send url to localhost
server by cliking send-button
the url changed localhost:8080/login, it should be localhost:8080/folder1/login
how I can keep subfolder name in url?
and I want convert urls like this: www.best-food-of-the-usa.com/index.php?operation=top&state=arizona&
city=mesa&limit=10
to like this: www.best-food-of-the-usa.com/arizona/mesa/top10.html
Any help is appreciated. Thanks
\Jose
RewriteRule is behaving as the docs say.
From RewriteRule Directive Apache Docs
What is matched?
In VirtualHost context, The Pattern will initially be matched against the part of the URL after the hostname and port, and before the query string (e.g. "/app1/index.html").
In Directory and htaccess context, the Pattern will initially be matched against the filesystem path, after removing the prefix that lead the server to the current RewriteRule (e.g. "app1/index.html" or "index.html" depending on where the directives are defined).
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.
So, when in Directory and htaccess context the prefix /www/folder1/ will be removed. Also remember when matching with RewriteRule in Directory and htaccess context, the pattern will never begin with /.
So, your RewriteRule Should be:
Options +FollowSymLinks
RewriteEngine On
RewriteRule (.*) /index.php?disp=$1 [QSA,L]

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>