Forum Moderators: phranque

Message Too Old, No Replies

Need Help For Redirect With Spaces (%20%20) At End

         

Planet13

8:19 pm on Jul 22, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Someone links to my site with Two empty spaces and I would like to do a proper 301 redirect to the actual page.

Google WMT says that they link to:

http://www.example.com/page.htm.%20%20


Which returns a 404 error

So how would I do a redirect / rewrite to

http://www.example.com/page.htm


Thanks in advance.

penders

9:10 pm on Jul 22, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ironically, if these were real spaces (ie. not encoded) at the end of the HREF attribute then most browsers seem to strip them before making the request. However, you also seem to have an erroneous period at the end as well?

To remove any period and two white-space chars at the end of a URL, try in .htaccess ...

RewriteEngine On
RewriteRule ^(.*)\.\s\s$ /$1 [R=301,L]


The pattern is matched against the %-decoded URL path.

Planet13

9:38 pm on Jul 22, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi there, Penders:

Thanks so much, that worked, but I just realized I had a TOTAL BRAIN CRAMP and that I actually need it to redirect to:

http://www.example.com/some_other_page_altogether/


Is there a way to do that?

The new page doesn't have an extension and ends in a trailing slash (it is a wordpress page).

Thanks in advance.

lucy24

9:56 pm on Jul 22, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The easy way is to simply omit the closing anchor from the existing redirect. Presumably you've already got one, if the URL has changed. Then it doesn't matter if visitors request
blahblah.html
or
blahblah.html.fzzt.whoops!jiggerypokery
because they'll get redirected either way.

For the generic extra-stuff-after-the-extension issue, the pattern is

\.html.

without closing anchor, where . means "any character in the world" and it doesn't matter if there's more of 'em. It's then your choice whether to redirect to the proper form, or 403 outright (if you suspect they're up to no good), or some page-specific fix.

For example (just one of many possible approaches)

RewriteCond %{REQUEST_URI} ^(.+\.html)
RewriteRule \.html. http://www.example.com/%1 [R=301,L]


where the sole purpose of the condition is to save your server the work of capturing on every single request ever.

penders

10:25 pm on Jul 22, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



@lucy24 - yes, like it!

Although I guess if we are literally redirecting "page.htm" (which admittedly I'd taken to mean any page; or is it literally any page?) to "some_other_page_altogether/" then we probably don't need to capture anything after all... (?)

RewriteRule ^page\.htm. http://www.example.com/some_other_page_altogether/ [R=301,L]

[edited by: penders at 10:35 pm (utc) on Jul 22, 2014]

Planet13

10:31 pm on Jul 22, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Oh boy, that makes my head hurt.

I'm sorry, but did I mention that I am a product of the California Public Education System? (Reaches for very big bottle of Centrum silver and even bigger bottle of Jameson's)

I know I ask a lot of you already, but is there a way you could simplify it down to my specific example?

Otherwise, it is very likely that I will insert the line:

blahblah.html.fzzt.whoops!jiggerypokery

verbatim into my .htaccess file, and I SORT OF get the feeling that was meant to be poetic liberty as opposed to an actual piece of code I am supposed to use...

lucy24

11:52 pm on Jul 22, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



if we are literally redirecting "page.htm" (which admittedly I'd taken to mean any page; or is it literally any page?) to "some_other_page_altogether/" then we probably don't need to capture anything after all... (?)

Right. I was showing the generic form, where you're simply redirecting to the same URL minus the extraneous stuff. It's essentially the same principle as when you redirect an explicit "index.html" to the identical URL minus "index.html". Even google falls victim to this extraneous-extra-nonsense sometimes when they follow a sloppily coded link. In fact most of the time the "ignore them until they go away" approach works just fine. It's only when a desirable link includes an error that you have to take action.

The quoted passage about "any page" makes no sense without its italics, but oh well.

did I mention that I am a product of the California Public Education System?

Me too, so no use hiding behind that. Admittedly, I sneaked in under the wire before K-12 got Jarvis'd to death, so there's that.

You can use the lines I cited under "for example" as your catch-all. But if you're already redirecting some specific URL like

RewriteRule somepage\.html http://www.example.com/otherpage [R=301,L]

keep that redirect. Just make sure there isn't a $ ending anchor after the ".html".

Hasty edit: Oh yes and don't quote me on the %{REQUEST_URI} because I can never remember if this specific form has a leading / or not.

Planet13

5:33 am on Jul 23, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ok, I think I understand. So if I want to redirect:

http://www.example.com/blue_widgets_2013.htm.%20%20

to this page:

http://www.example.com/2014_blue_widgets/


I should do something basically like this:

RewriteRule ^blue_widgets_2013\.htm. http://www.example.com/2014_blue_widgets/ [R=301,L]

Is that right?

penders

6:59 am on Jul 23, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes, that looks OK. Redirect one URL (with any trailing characters) to another.

Planet13

3:57 pm on Jul 23, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks for the confirmation, penders. Will do this today.

lucy24

6:10 pm on Jul 23, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If you are also redirecting
blue_widgets_2013.htm
(without errors in URL)
to
2014_blue_widgets/
then you should omit the . after "htm" so everyone gets redirected, whether their initial request was right or wrong.

Planet13

7:53 pm on Jul 23, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



then you should omit the . after "htm" so everyone gets redirected, whether their initial request was right or wrong.


Ugghh... I thought the [unescaped] dot after the htm was necessary to capture the people who came though the link with the %20%20 appended to the .htm

So can I have a simple 301 redirect for those who came from the CORRECT old URL, and the other redirect for those who came from the incorrect URL?

lucy24

8:14 pm on Jul 23, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If you omit a closing anchor, you should be able to use the SAME redirect for both.

That's assuming I've read everything right and the two types of requests are for
/oldURL.html
and
/oldURL.htmlblahblah
i.e. the same URL apart from the added stuff.

Planet13

8:26 pm on Jul 23, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes.

Two types of request.

Most are for:

/oldURL.htm

and others for:

/oldURL.htm%20%20

penders

8:35 pm on Jul 23, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



RewriteRule ^blue_widgets_2013\.htm. http://www.example.com/2014_blue_widgets/ [R=301,L]


then you should omit the . after "htm" so everyone gets redirected


... to use the SAME redirect for both.


RewriteRule ^blue_widgets_2013\.htm http://www.example.com/2014_blue_widgets/ [R=301,L]


Et voilą.

Planet13

11:57 pm on Jul 23, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



thanks for all the help!

I am going to try it tomorrow. you wouldn't believe what a bad day it's been. considering how half the time I make a change to my. htaccess file I end up chunking my whole site, I should probably hold off for now.

lucy24

2:14 am on Jul 24, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



considering how half the time I make a change to my .htaccess file I end up chunking my whole site

That's why a test site-- on the same server-- is worth so much more than the $9.95 you pay for the name. Want to try something and you're not sure it will work as intended? Pop it into your test site's htaccess where no humans will be harmed.