How to rewrite % signs in URLs

ISAPI_Rewrite is Apache mod_rewrite compatible URL rewriter for Microsoft IIS
User avatar
Posts: 28
Joined: 29 Aug 2012, 06:15

How to rewrite % signs in URLs

23 Sep 2013, 12:39

Hi,

Can someone please tell me how to rewrite % symbols in URLs. We need to rewrite URLs like this:

/template.asp?price=409.00&width=70%&height=90%

to:

/template.asp?price=409.00&width=70%25&height=90%25

So basically we need to change all %'s to %25 before the page is served.

Thanks,
Mark

User avatar
Posts: 28
Joined: 29 Aug 2012, 06:15

Re: How to rewrite % signs in URLs

24 Sep 2013, 11:02

Please, does someone have a hint? Tried lots of different things but nothing will match my URLs.

User avatar
Posts: 1264
Joined: 07 Mar 2012, 10:16

Re: How to rewrite % signs in URLs

24 Sep 2013, 13:27

Hello,

Try something like:

Code: Select all
RewriteEngine on
RewritreBase /

RewriteCond %{QUERY_STRING} ^price=409\.00&width=70\%&height=90\%$
RewriteRule ^template\.asp /template.asp?price=409.00&width=70\%25&height=90\%25? [NC,R=301,L]


Regards
Andrew

User avatar
Posts: 28
Joined: 29 Aug 2012, 06:15

Re: How to rewrite % signs in URLs

27 Sep 2013, 04:00

Hi Andrew, thank you for the reply.

The issue I have is that I have lots of these URLs, so I need to remove all the %'s and replace them with %25. How can I do that?

User avatar
Posts: 1264
Joined: 07 Mar 2012, 10:16

Re: How to rewrite % signs in URLs

27 Sep 2013, 09:14

are they all with template.asp? are all '%' located in querystring?

Regards
Andrew

User avatar
Posts: 28
Joined: 29 Aug 2012, 06:15

Re: How to rewrite % signs in URLs

27 Sep 2013, 16:05

Andrew, they are in various folders and templates, not just template.asp. The % are only in the CGI params (in querystring).

Regards,
Mark

User avatar
Posts: 28
Joined: 29 Aug 2012, 06:15

Re: How to rewrite % signs in URLs

29 Sep 2013, 17:55

Andrew, is it possible to just change %'s in the query string only, and change them to some other string?

User avatar
Posts: 1264
Joined: 07 Mar 2012, 10:16

Re: How to rewrite % signs in URLs

29 Sep 2013, 23:02

You should be able to do it using loop-rule. It's [LP] flag.

Code: Select all
RewriteEngine on
RewritreBase /

RewriteCond %{QUERY_STRING} ^([^\%]+)\%(&.*)$
RewriteRule ^(.*) /$1?%1\%25%2? [NC,LP,R=301,L]


Regards
Andrew

User avatar
Posts: 28
Joined: 29 Aug 2012, 06:15

Re: How to rewrite % signs in URLs

30 Sep 2013, 05:14

Hi Andrew, that rewrite just results in a 404 error for a URL such as:

http://www.mydomain.com/somefolder/some ... e&width=70%&height=90%

I need this URL to become:

http://www.mydomain.com/somefolder/some ... ight=90%25

Seems simple, but isn't :-(

User avatar
Posts: 1264
Joined: 07 Mar 2012, 10:16

Re: How to rewrite % signs in URLs

30 Sep 2013, 07:16

Well, it results in 404, but are there any changes in the URL? did redirect occur? or it didn't occur at all?

User avatar
Posts: 28
Joined: 29 Aug 2012, 06:15

Re: How to rewrite % signs in URLs

30 Sep 2013, 07:43

Ok, the URL comes back as:

http://www.mydomain.com//test1/test2/te ... ight=90%25

So rewriting is taking place, resulting in a 404. Getting two slashes after the domain and the first CGI argument has been removed. It should be:

http://www.mydomain.com/test1/test2/tes ... ight=90%25

User avatar
Posts: 1264
Joined: 07 Mar 2012, 10:16

Re: How to rewrite % signs in URLs

30 Sep 2013, 23:28

So it does work... just not the way we want...
Lets try this, and see what'll happen


Code: Select all
RewriteCond %{QUERY_STRING} ^([^\%]+)\%(&.*)$
RewriteRule ^(.*) $1\?%1\%25%2? [NC,LP,R=301,L]

User avatar
Posts: 28
Joined: 29 Aug 2012, 06:15

Re: How to rewrite % signs in URLs

01 Oct 2013, 06:40

Andrew, that is better thanks...

Now I get my URL rewritten

http://www.mydomain.com/test/test2/test ... ight=90%25

For some reason the first % argument is converted to %2525, whilst the second just %25. The latter is correct, the former is not.

In Internet Explorer 9 the URL will not load and gives a 404 when the redirect occurs btw. The rewrite works in Chrome and FireFox, but not in MSIE. Most of our visitors are using MSIE :(

Thanks,
Mark

User avatar
Posts: 1264
Joined: 07 Mar 2012, 10:16

Re: How to rewrite % signs in URLs

01 Oct 2013, 10:04

Well... I'd think it's escaping, but it happens only to one part...

So my assumption would be that this issue occurs when th rules runs the second time. Can you confirm? Try to request a URL with only one '%'.


Regards
Andrew

User avatar
Posts: 1264
Joined: 07 Mar 2012, 10:16

Re: How to rewrite % signs in URLs

01 Oct 2013, 10:06

...and browsers. We're doing the server side redirects. Which means that sever treats everyone just the same. So the problem is in broser(caching, headers, handling responses, etc). There's nothing we can do here.

User avatar
Posts: 28
Joined: 29 Aug 2012, 06:15

Re: How to rewrite % signs in URLs

01 Oct 2013, 10:44

Hi, yes it is ok with one parameter, as you say.

If MSIE does not work and Chrome/FireFox/Safari/Opera/Silk does, why is that? I wonder why IE does not like the resulting 301'd URL?

User avatar
Posts: 1264
Joined: 07 Mar 2012, 10:16

Re: How to rewrite % signs in URLs

02 Oct 2013, 09:38

Hm......

LEts try:

Code: Select all
RewriteCond %{QUERY_STRING} ^([^\%&]+)\%(&.*)$
RewriteRule ^(.*) $1\?%1\%25%2? [NC,LP,R=301,L]

User avatar
Posts: 28
Joined: 29 Aug 2012, 06:15

Re: How to rewrite % signs in URLs

02 Oct 2013, 13:41

That doesn't seem to do anything, the same URL comes back unchanged (all browsers).

Have we, gasp, found something that Rewriter 3.0 cannot do!? Surely not :)

User avatar
Posts: 1264
Joined: 07 Mar 2012, 10:16

Re: How to rewrite % signs in URLs

02 Oct 2013, 14:36

Don't call me Surely! (sorry, couldn't resist. It's the joke in the office when someone starts sentence with "Surely, ..")


Well, I just don't get it.It should work. What we do - we divide querystring into 2 parts based on the condition that it meets "%&" and if it does, we take the firsat part of querystring + add "%25&" instead of "%" + second part. And it all goes in a loop, untill no "%&" is found

I just don't understand why when it runs the second time, it take "%25&" and transforms it again...

EUREKA! It seems like there's something related to the escaping the characters. Try to use [NE] flag in this rule.

Regards
Andrew

User avatar
Posts: 28
Joined: 29 Aug 2012, 06:15

Re: How to rewrite % signs in URLs

02 Oct 2013, 16:41

Tried replacing NC with NE and same result - same URL comes back. Here's the URL I request, and the same comes back:

Code: Select all
http://www.mydomain.com/test/test2/test.asp?price=100.00&mode=true&width=70%&height=90%


The rules I used:

Code: Select all
RewriteCond %{QUERY_STRING} ^([^\%&]+)\%(&.*)$
RewriteRule ^(.*) $1\?%1\%25%2? [NE,LP,R=301,L]


Is that use of NE correct? No %25's are placed in the URL at all now, indeed it does not seem if the URL is now rewriting at all :(

Next

Return to ISAPI_Rewrite 3.0

Who is online

Users browsing this forum: No registered users and 23 guests