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.
Untested (updated: tested, does work): RewriteEngine on RewriteRule [^\s]+ $0?%{QUERY_STRING} Code (markup): 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?
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.
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
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>
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.
Try replacing every occurance of %20 with a backslash (to escape the space): Example: redirect 301 /source.htm\ \ \ /destination.htm Code (markup): 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 Code (markup):
ahh, there we go. Used the quotes as in the second example and it works perfectly. Thanks for your help.
If you want to get rid of one or more spaces, you could use this: RedirectMatch 301 "^(.+?) +$" $1 Code (markup): J.D.
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?
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.
We had the same problem (and thanks for the solution!). A blogger (here: http://nbmaa.wordpress.com/2011/08/03/the-art-and-function-of-carved-birds/ ) linked to our site with this link: <a href="http://www.birdsofvermont.org/ ">Birds of Vermont Museum</a> Code (markup): Firefox9 wasn't stripping the slashes, nor was Googlebot (I noticed it with google webmaster tools, which showed a crawl error). Now at least FF is behaving. As far as I can tell Chrome 14, IE9, and Safari5 (all on Win7) were behaving already; they certainly are behaving correctly with this fix. Thanks again for posting. (p.s. sorry the links aren't live; I'm too new to these forums)
It works if you use "" for example out this in htaccsess: redirect 301 "/Contact Us.htm" http://www.yourdomain.com/contact Thats redirecting Contact Us (which has %20 in space) to contact