Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite with a ampersand (%26) in url

ampersand, %26, php

         

sporb

12:40 pm on Jan 18, 2008 (gmt 0)

10+ Year Member



I am doing a mod rewrite and it works great, but when there is a ampersand %26 in the url, it breaks. I have tried the "ne" flag, but it didn't help and i can't find any other solutions.

i have tried search.php?s=this+%26+that and it picks everything up fine, the problem occurs when a url like /search/this+%26+that/ is used.

here is my rewrite:

RewriteRule ^search/(.*)/$ /search.php?s=$1 [NC]

jdMorgan

1:31 pm on Jan 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



According to the HTTP specification [w3.org], ampersands are not allowed in the URL-part, only in the appended query string. So be aware that although you may be able to fix this for your browsers and your server, you may reasonably expect future problems with other browsers and search engine spiders if you stick with this URL-syntax.

The usual fix is to use the [NE] flag on the RewriteRule, but I strongly recommend that you follow the HTTP specification and eliminate the ampersand (and any other reserved characters) from your static/SE-friendly URLs. Consider using slashes, hyphens, underscores, and/or periods instead, as these characters are unreserved.

Jim

karabas

5:48 am on Jan 22, 2008 (gmt 0)

10+ Year Member



My question is kind of similar. I would like to achieve the following:

create rule/ruleset that would change

http://www.example.com/url/http://www.test.com/page1.php?p1=a&p2=b

to

http://www.example.com/url.php?url=http://www.test.com/page1.php%3fp1=a%26p2=b

so that if I wrote the following code in url.php

<?=$_SERVER['QUERY']?>

I would get

url=http://www.test.com/page1.php%3fp1=a%26p2=b

as the content of the page?

I tried
RewriteRule ^url/(.+)$ /url.php?url=$1 [QSA]

and the result is
url=http://www.test.com/page1.php&p1=a&p2=b

Is there any way to achieve it?

jdMorgan

2:31 pm on Jan 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What you are trying to do is also invalid. Either change the characters so that you are not trying to embed "?" and "&" in the URL produced by your PHP script (use other non-reserved characters instead), or construct the URL with valid query parameters in PHP from the start.

The best advice I can offer in this thread is this: Do not try to fight the HTTP protocol specification; You will cost yourself a lot of time, money, and frustration dealing with compatibility problems. If you want your site to work properly now and in the future, and you want to achieve and maintain good search ranking, stick with the standards.

See the following:
RFC2396 - Uniform Resource Identifiers (URI): Generic Syntax [faqs.org]
RFC2616 - HTTP/1.1 Protocol [w3.org]

Jim