Redirecting the url using query string parameters

ISAPI_Rewrite is Apache mod_rewrite compatible URL rewriter for Microsoft IIS
User avatar
Posts: 8
Joined: 13 Aug 2012, 07:01

Redirecting the url using query string parameters

21 Aug 2012, 08:55

Hi,

Please help me in redirecting the following url

https://domain.com/sales/abc/opps/edit.aspx?id={66740FEF-A424-4854-8239-38DD3E43FA16}
to
https://domain.com/testsales/testopps/main.aspx?oppid={66740FEF-A424-4854-8239-38DD3E43FA16}

The above url contains query string and it is using https. It should redirect to different folder using https and should carry the query string parameter.

Thanks
Rakesh.

User avatar
Posts: 871
Joined: 12 Mar 2012, 09:54

Re: Redirecting the url using query string parameters

21 Aug 2012, 09:38

Hello, Rakesh

Please try it like this:

RewriteBase /
RewriteCond %{HTTPS} .*on.* [NC]
RewriteCond %{QUERY_STRING} ^id=(\{.*\})$ [NC]
RewriteRule ^sales/abc/opps/edit\.aspx$ https\://domain.com/testsales/testopps/main.aspx?oppid=%1 [NC,R=301,L]

User avatar
Posts: 8
Joined: 13 Aug 2012, 07:01

Re: Redirecting the url using query string parameters

22 Aug 2012, 06:09

Hello Anton,

Thanks for the solution. I have one more question.

I wrote a rule as given below and it is working fine if there is a request domain.com/sales/abc/opps/edit.aspx

RewriteCond %{HTTPS} .*on.* [NC]
RewriteRule ^sales/abc/opps/edit\.aspx$ testsales/testopps/main.aspx?oppid= [NC,R,L]

but if there is a request which contains query string (as shown below)

domain.com/sales/abc/opps/edit.aspx?id=46 then the above rule gets passed which should not.

My request url may or may not contain query string. Basically I am looking for the below scenarios
for example:
1) https://domain.com/sales/abc/opps/edit.aspx should redirect to
https://domain.com/testsales/testopps/main.aspx?oppid=
2) https://domain.com/sales/abc/opps/edit.aspx?prm=3&cat=4
to
https://domain.com/testsales/testopps/m ... id=3&cat=4
3) https://domain.com/sales/abc/opps/edit.aspx?id=3 this should not redirect to any thing. it should behave as it is.

Please help...

User avatar
Posts: 871
Joined: 12 Mar 2012, 09:54

Re: Redirecting the url using query string parameters

22 Aug 2012, 07:34

Ok, please try to fix it like this:

RewriteCond %{HTTPS} .*on.* [NC]
RewriteCond %{QUERY_STRING} ^(?:prm=(\d+&cat=\d+))?$ [NC]
RewriteRule ^sales/abc/opps/edit\.aspx$ https://domain.com/testsales/testopps/main.aspx?oppid=(?%1%1) [NC,R=301,L]

User avatar
Posts: 8
Joined: 13 Aug 2012, 07:01

Re: Redirecting the url using query string parameters

22 Aug 2012, 13:41

Could you please explain me the redirect rule that you provided.

^(?:prm=(\d+&cat=\d+))?$ --> How does this work?
(?%1%1) --> How does this work.

User avatar
Posts: 871
Joined: 12 Mar 2012, 09:54

Re: Redirecting the url using query string parameters

23 Aug 2012, 03:58

Sure:
^(?:prm=(\d+&cat=\d+))?$ --> the outer parentheses with the question mark make the query string optional. The inner parentheses match the necessary part of the query string if it's there.
(?%1%1) --> sort of IF construction: IF %1 exists (meaning, if there's a query string), then add what was matched here (\d+&cat=\d+) to the URL.

User avatar
Posts: 8
Joined: 13 Aug 2012, 07:01

Re: Redirecting the url using query string parameters

28 Aug 2012, 06:56

Thanks for the information. It was really helpfull.

I am facing another problem.
In the requested url, query string parameter FromId's value is %7b998 which is converting to %257b998. I want it to be the same as in the requested url. I tried putting 'O' in the end of the rule but didn't work.

Can you please help me how to get it?

RewriteCond %{HTTPS} .*on.* [NC]
RewriteCond %{QUERY_STRING} ^FromType=(.*)&FromId=(.*)$ [NC]
RewriteRule ^sales/abc/opps/edit\.aspx$ testsales/testopps/main.aspx?oppid=&FromType=%1&FromId=%2 [NC,R=301,L]

User avatar
Posts: 871
Joined: 12 Mar 2012, 09:54

Re: Redirecting the url using query string parameters

28 Aug 2012, 07:34

Please try to use NE flag after the rule.

User avatar
Posts: 8
Joined: 13 Aug 2012, 07:01

Re: Redirecting the url using query string parameters

28 Aug 2012, 07:51

No, it didn't work.

RewriteRule ^sales/abc/opps/edit\.aspx$ testsales/testopps/main.aspx?oppid=&FromType=%1&FromId=%2 [NC,R=301,L,NE]

User avatar
Posts: 8
Joined: 13 Aug 2012, 07:01

Re: Redirecting the url using query string parameters

28 Aug 2012, 08:02

Sorry..It worked..
Thanks a lot.

User avatar
Posts: 16
Joined: 13 Nov 2012, 00:19

Re: Redirecting the url using query string parameters

15 Nov 2012, 22:23

I have tried this string and it worked. Thanks.

Return to ISAPI_Rewrite 3.0

Who is online

Users browsing this forum: No registered users and 31 guests