1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to 301 Redirect URL with %20 In It

Discussion in 'Apache' started by chachi, Feb 22, 2005.

  1. #1
    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.
     
    chachi, Feb 22, 2005 IP
  2. nullbit

    nullbit Peon

    Messages:
    489
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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?
     
    nullbit, Feb 22, 2005 IP
  3. J.D.

    J.D. Peon

    Messages:
    1,198
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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.
     
    J.D., Feb 22, 2005 IP
  4. nullbit

    nullbit Peon

    Messages:
    489
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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.
     
    nullbit, Feb 22, 2005 IP
  5. chachi

    chachi The other Jason

    Messages:
    1,600
    Likes Received:
    57
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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
     
    chachi, Feb 22, 2005 IP
  6. nullbit

    nullbit Peon

    Messages:
    489
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Do your CSS files have spaces in them? If yes, then the RewriteRule, will need a small revision.
     
    nullbit, Feb 22, 2005 IP
  7. chachi

    chachi The other Jason

    Messages:
    1,600
    Likes Received:
    57
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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>
     
    chachi, Feb 22, 2005 IP
  8. J.D.

    J.D. Peon

    Messages:
    1,198
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Can you post a sample URL/user agent from the log?

    J.D.
     
    J.D., Feb 22, 2005 IP
  9. nullbit

    nullbit Peon

    Messages:
    489
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #9
    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.
     
    nullbit, Feb 22, 2005 IP
  10. chachi

    chachi The other Jason

    Messages:
    1,600
    Likes Received:
    57
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I know how to do a 301 in .htaccess. But, it is not working with %20 in the url
     
    chachi, Feb 22, 2005 IP
  11. nullbit

    nullbit Peon

    Messages:
    489
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #11
    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):
     
    nullbit, Feb 22, 2005 IP
  12. chachi

    chachi The other Jason

    Messages:
    1,600
    Likes Received:
    57
    Best Answers:
    0
    Trophy Points:
    0
    #12
    ahh, there we go. Used the quotes as in the second example and it works perfectly. Thanks for your help.
     
    chachi, Feb 22, 2005 IP
  13. J.D.

    J.D. Peon

    Messages:
    1,198
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #13
    If you want to get rid of one or more spaces, you could use this:

    RedirectMatch 301 "^(.+?) +$" $1
    Code (markup):
    J.D.
     
    J.D., Feb 22, 2005 IP
  14. poit

    poit Guest

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Heck that is interesting.
    Thanks for that
     
    poit, Feb 23, 2005 IP
  15. TechEvangelist

    TechEvangelist Guest

    Messages:
    919
    Likes Received:
    140
    Best Answers:
    0
    Trophy Points:
    133
    #15
    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?
     
    TechEvangelist, Feb 23, 2005 IP
  16. J.D.

    J.D. Peon

    Messages:
    1,198
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #16
    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.
     
    J.D., Feb 23, 2005 IP
  17. metasilk

    metasilk Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #17
    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)
     
    metasilk, Jan 3, 2012 IP
  18. Fiverscripts

    Fiverscripts Moderator Staff

    Messages:
    1,839
    Likes Received:
    42
    Best Answers:
    1
    Trophy Points:
    370
    #18
    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
     
    Fiverscripts, Feb 24, 2012 IP