Mod_Rewrite when URLs contain ampersands in the query

I get the following error when the search query in my URL contains an ampersand.

Error: 404 - File not found on this server.
[an error occurred while processing this directive]

Here is an example URL that produces this error:

http://example.com/basic/peace+%26+calming

Here is what I have in my .htaccess file that I need help with:

# Basic search
RewriteRule ^basic/([a-zA-Z0-9\+]+)$ aromatherapy.php?t=basic&q=$1&x=$2 [L]
RewriteRule ^basic/([a-zA-Z0-9\+]+)/$ aromatherapy.php?t=basic&q=$1&x=$2 [L]
RewriteRule ^basic/([a-zA-Z0-9\+]+)/([a-zA-Z0-9_\+]+)$ aromatherapy.php?t=basic&q=$1&x=$2 [L]

How do I write the rule so that it accommodates urlencoded search queries? Also, is there a way to combine the first two rules? The only difference is that the second one accommodates a trailing slash if the user has one in the URL.

Thanks!

bb,

Your & and + are reserved characters:

[quote=https://www.ietf.org/rfc/rfc2396.txt]2.2. Reserved Characters

Many URI include components consisting of or delimited by, certain
special characters. These characters are called “reserved”, since
their usage within the URI component is limited to their reserved
purpose. If the data for a URI component would conflict with the
reserved purpose, then the conflicting data must be escaped before
forming the URI.

  reserved    = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" |
                "$" | ","

The “reserved” syntax class above refers to those characters that are
allowed within a URI, but which may not be allowed within a
particular component of the generic URI syntax; they are used as
delimiters of the components described in Section 3.[/quote]

Since I’m not clairvoyant, I have no idea why you’re using such a scheme for your URIs. I find it particularly odd that your $2 values for x= are undefined (for the first two RewriteRules. In addition, you should NEVER use the same code with an optional trailing / as the internal links will be shifted (directory level) after the redirection.

http://dk.co.nz/seo for a rather lengthy tutorial with plenty of coding examples.

Regards,

DK

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.