Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite problem

problem with '&' even when using urlencode

         

doc_z

3:29 pm on Mar 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm using a mod_rewrite of the following form:

RewriteCond %{REQUEST_URI} /test/(.*)\.html$
RewriteRule .* /test/index.php?page=%1 [L]

In general it's working fine. However, a problem appears when a '&' occurs, although I urlencoded the URL. For example '/test/A%26B.html' produces an error in my PHP script. The reason seems to be that the URL is rewritten to '/test/index.php?page=A&B' instead of '/test/index.php?page=A%26B'.

Is this a general problem or just a configuration problem and how can it be solved?

jdMorgan

4:52 pm on Mar 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



doc_z,

It may be a technical problem, since "&" is not allowed in a URI -- It *is* allowed in a query string, but not in the actual URI -- that is, anything preceding a "?" character.

The usual way to map SE-friendly URIs to query strings is to separate the query string parameters with URI-legal characters such as slashes, hyphens, underscores, etc., and then use mod_rewrite (or similar) to change those to query-string parameter separators. For example:


/test/A/B.html -> /test/index.php?page=A&B

RewriteRule ^test/([^/]*)/([^.]*)\.html$ /test/index.php?page=%1&%2 [L]


Ref: [faqs.org...] Section 2.2

That said, you may be able to get it working with certain server configurations and scripting languages, but using reserved characters in URIs is definitely not recommended if wide compatibility is of value.

Jim

doc_z

5:33 pm on Mar 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



jdMorgan,

it seems that I haven't described my problem correctly.

I know that some characters are causing problems in the URI. Therefore, I urlencode my strings in PHP, e.g. <a href="/test/'.urlencode($string).'.html">. This should be rewritten to '/test/index.php?page=urlencoded_string'. However, in case of $string="A&B" the URL '/test/A%26B.html' is rewritten to '/test/index.php?page=A&B' instead of '/test/index.php?page=A%26B'. The latter case is what I want and what I have expected. Unfortunately, it seems that the string is urldecoded.