Spaces in URL with Mod Rewrite

I am currently rewriting

http://mysite.com/page/something

to http://mysite.com/page.php?foo=something

When I try http://mysite.com/page.php?foo=some thing
(note that there’s a space), it seems to work (converted to %20). However, when I try http://mysite.com/page/some thing, I get a 400 Bad Request Error, providing me the following message:

Your browser sent a request that this server could not understand.

The request line contained invalid characters following the protocol string.

How can I make the server understand the space character through mod_rewrite? I really want to get space characters to work to allow my visitors (mostly kids) to directly enter a name without worrying about any special characters to type instead of spaces (plus signs or underscores, for example).

wei,

Gee, you STILL haven’t read the tutorial in my signature, have you. This very subject (spaces in the {REQUEST_URI} and how to get around them) is covered.

Moreover, you MUST know that a space is FORBIDDEN in a URL - PERIOD! Slide on over to w3.org for confirmation of that - that’s why your space is replaced by %20 (hex for the space character).

Now, have your visitors enter their desired URL in a textbox and convert it before submitting and you should be okay (when you get it back as a string) OR have mod_rewrite convert it for you (bad idea - PHP is much more efficient at character manipulation).

Regards,

DK

Sorry, I must have overlooked that while reading the article (I did read it though :slight_smile: ). Could you please direct me to the section that talks about this issue?

So, suppose I DO want spaces (being plain stupid and ignoring the fact that spaces are illegal in URLs) since I expect that visitors may directly type the URL. How would I prevent that 400 Error? It seems that there’s something with mod_rewrite that prevents the spaces, while directly entering the query string in the URL allows it. I just want to see how this works. I really appreciate all your help and apologize for asking about this so many times. :smiley:

wei,

It’s in the mod-rewrite Regex section where I have the code:

$state = str_replace ( ' ', '_', $state );
AND
$state = str_replace ( '_', ' ', $state ); 

The ban on spaces is enforced by all browsers as they will convert every space to %20 before sending the request via the http protocol.

You CAN access a space in mod_rewrite but ONLY within a character range definition by an escaped space, i.e., [\_] (where the _ is a space).

A query string created by a form will have replaced its spaces with +'s so, your regex could use code similar to the newly added section: Replace A Character:

RewriteEngine on
RewriteRule ^/?([^.]*)[\\ ]([^.]*)$ $1+$2 [N,L]
RewriteRule ^/?([^.]+)$ index.php?$1 [R=301,L]

Regards,

DK