Debt Consolidation - Deaf Topics - Find jobs - Online Name - Internet Advertising

PDA

View Full Version : How to 301 Redirect URL with %20 In It


chachi
Feb 22nd 2005, 8:21 am
Anyone know how to redirect a url with spaces in them. I get more and more 404 pages because of urls with %20 after them these days. I can't seem to get them to work in my .htaccess file.

nullbit
Feb 22nd 2005, 11:07 am
Untested (updated: tested, does work):

RewriteEngine on

RewriteRule [^\s]+ $0?%{QUERY_STRING}


Tell me if it does not work. This will just remove the spaces from the url, you don't really need to perform a redirect, unless you are worried about it being indexed as duplicate content?

J.D.
Feb 22nd 2005, 12:04 pm
Anyone know how to redirect a url with spaces in them. I get more and more 404 pages because of urls with %20 after them these days. I can't seem to get them to work in my .htaccess file.Browsers usualy strip the whitespace at the end of the URL. Look at the user-agent strings in the log to figure out who's requesting these URLs.

J.D.

nullbit
Feb 22nd 2005, 12:17 pm
Browsers usualy strip the whitespace at the end of the URL. Look at the user-agent strings in the log to figure out who's requesting these URLs.

J.D.

I was thinking this. Chances are it might be a good thing to not serve this user agent, since it's probably not a regular user, most likely some badly coded scraper.

chachi
Feb 22nd 2005, 1:35 pm
I am not really doing it for browsers as much as I am for bots. Gbot in particular does not seem to want to remove the spaces.

That rewrite removed the spaces, but it also made it so that my style sheets were not used by FF

nullbit
Feb 22nd 2005, 1:36 pm
Do your CSS files have spaces in them? If yes, then the RewriteRule, will need a small revision.

chachi
Feb 22nd 2005, 1:47 pm
no spaces in the file names

<edit>Ahh, but if I turn the RewriteEngine off, it works. :)

But, I really would like to do a 301 as these are links coming from other sites, so if anyone knows how to do that I would love to hear how.
</edit>

J.D.
Feb 22nd 2005, 2:10 pm
Can you post a sample URL/user agent from the log?

J.D.

nullbit
Feb 22nd 2005, 2:15 pm
A 301 using .htaccess is simple:

redirect 301 /source.htm /destination.htm

Somehow you need to combine that with the rewrite module to capture & redirect all pages with trailing spaces. Right now, I can't think how you would do this.

Edit: Of course, if the number of URLs is short, you could just add them all using the above sample.

chachi
Feb 22nd 2005, 2:38 pm
I know how to do a 301 in .htaccess. But, it is not working with %20 in the url

nullbit
Feb 22nd 2005, 2:46 pm
I know how to do a 301 in .htaccess. But, it is not working with %20 in the url
Try replacing every occurance of %20 with a backslash (to escape the space):

Example:

redirect 301 /source.htm\ \ \ /destination.htm

Remember to include an unescaped space at the end of the source string.

Also using literal spaces, and quoting the source string might work:

Example:

redirect 301 "/source.htm " /destination.htm

chachi
Feb 22nd 2005, 5:52 pm
ahh, there we go. Used the quotes as in the second example and it works perfectly. Thanks for your help.

J.D.
Feb 22nd 2005, 7:35 pm
redirect 301 "/source.htm " /destination.htm
If you want to get rid of one or more spaces, you could use this:

RedirectMatch 301 "^(.+?) +$" $1J.D.

poit
Feb 23rd 2005, 5:37 am
Heck that is interesting.
Thanks for that

TechEvangelist
Feb 23rd 2005, 7:22 am
One piece was missing in this thread. How were the spaces getting tacked to the end of the URLs? I've never seen a hex space %20 tacked onto the end of a URL, although it is common when someone uses a space in a file name.

How is it gettin in there?

J.D.
Feb 23rd 2005, 7:41 am
How is it gettin in there?I'd be interested to hear about this as well. chachi, can you post a couple of user agents (from your logs) that append spaces to URLs?

J.D.