Forum Moderators: phranque

Message Too Old, No Replies

rewrite spaces in url

changing " " to "%20"

         

bradgrafelman

12:32 am on Sep 10, 2004 (gmt 0)

10+ Year Member



Ok. You all know that with these wonderful internet browsers like IE, that when you type a space in the URL, the program encodes that space to be "%20".

It encodes it, because spaces throw off the webserver when it tries to identify the protocol in the GET request. Especially on my windows server.

I have a %n link from my AIM profile, but for those people with spaces, AIM doesn't encode that space to "%20". My server receives that GET request with a space, and rejects it, saying invalid characters after protocol.

Does anyone know a simple way to use the mod_rewrite to change any and all " " (spaces) to "%20" (the encoded space)?

I just need a simple condition that does that. Doesn't have to be fancy. Doesn't have to do it only for AIM, or anything. I just want it to replace spaces with %20.

If anyone can tell me the expression I need, and all the places in my Apache configuration I need modify, it'd be greatly appreciated.

I'm running Apache v1.3.31 on Windows XP Home.

jdMorgan

1:32 am on Sep 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



bradgrafelman,

Welcome to WebmasterWorld!

A simple way? -- No, it's not simple, but there is a way.

This thread [webmasterworld.com] outlines several techniques for replacing any number of underscores with hyphens, and is adaptable to your problem.

Try it using "\ " or "\s" as the search pattern for a space, and %20 as the substitution. Additionally, you will have to use the [NE] flag on the RewriteRule to prevent the %20 itself from being escaped.

General reference material is cited in our forum charter.

Jim

londoh

12:20 am on Sep 11, 2004 (gmt 0)

10+ Year Member



Hello All

I've read and re-read the thread mentioned above and several others on this forum, as well as anything else I can find (forum charter etc) in an attempt to find a solution to rewriting spaces in URLs.

I have a site where URLs are generated by a script. I have changed that to generate URL's with hyphens instead of spaces. I would like to 301 the old URL with spaces to its new equivalent.

Several days ago I thought a had a solution. But it seems that only works on my test machine and not on the server.

The problem appears to be that I cannot find a regex that will match the space character.

I have tried " ", "\ ", "\s", "\040" "[:space:]", "[:blank:]"

In a simple test setup I am using the following in .htaccess

Options +FollowSymLinks
rewriteEngine On

RewriteCond %{REQUEST_FILENAME} \.(html)$
RewriteRule ^([^\s]*)\s(.*)$ [domain.com...] [R=301,L]

This works fine on my local server...

[domain.com...] in url.html
redirects to:
[domain.com...]

But on the net the same ruleset will replace an instance of any test character... EXCEPT a space.

Test Server is running apache 2, production server is running 1.3.27

I have root access on both machines.

Any assistance will be very much appreciated

Thanks

londoh

bradgrafelman

12:32 am on Sep 11, 2004 (gmt 0)

10+ Year Member



I found a working solution.

It may present some security vunerabilities, but it works for me.

Add this to the end of your Apache config file:

ProtocolReqCheck off

Worked for me.

SkyDog

12:46 am on Sep 12, 2004 (gmt 0)

10+ Year Member



I went through this same exercise a couple weeks ago. "\ " without the quotes matches the space character for mod-rewrite, but oddly not for mod-alias.

londoh

9:24 pm on Sep 13, 2004 (gmt 0)

10+ Year Member



Thanks for the replies.

I realise my post was not quite on topic for the thread.

In my case the solution below (which I take no credit for) worked:

Options +FollowSymLinks
rewriteEngine On

RewriteCond %{REQUEST_URI} ^(.*)\ (.*html)$
RewriteRule ^.*$ %1-%2 [E=space_replacer:%1-%2]
RewriteCond %{ENV:space_replacer}!^$
RewriteCond %{ENV:space_replacer}!^.*\ .*$
RewriteRule ^.*$ %{ENV:space_replacer} [R=301,L]

londoh