2

There are several links pointing to a site I manage in which the webmaster mistakenly included a space between the domain name and the page name:

    www.domain.com/ page.html

When the user clicks, this gives

    www.domain.com/%20page.html

I'd like to use mod_rewrite to redirect hits to the incorrect address to the correct address, but my rewrite rule is not working. I have tried the following without success:

    rewriterule ^\%20page.html$ /page.html [R=301,L]
    rewriterule ^.20page.html$ /page.html [R=301,L]

How can I write a rule to catch this address? I'd like to keep the PageRank and not be penalized for a broken link, and I can't get the webmaster to fix his links.

3
  • You have a webmaster that doesn't fix broken links? That seems like an impressively bad situation.
    – acrosman
    Apr 9, 2009 at 15:13
  • If the links are broken, I doubt you have a PageRank at all, so fix the links. If your webmaster won't fix them, that's a major problem. That's his job, and its inexcusable that he wrote them that way in the first place...
    – rmeador
    Apr 9, 2009 at 15:21
  • The links are from an external site with which I no longer have contact, and I'd prefer not to ask them -- they might remove the link instead of fixing it.
    – Andy Swift
    Apr 20, 2009 at 12:17

3 Answers 3

7

Use a literal space, escaped with a backslash so it doesn't end the regular expression:

RewriteRule ^\ page.html$ /page.html [R=301,L]
2

Put the space in your RewriteRule. Probably by the time mod_rewrite sees it, it's been decoded.

2
  • rewriterule ^.page.html$ /page.html [R=301,L] worked, thanks!
    – Andy Swift
    Apr 9, 2009 at 15:17
  • Yeah, you really ought to embed the space. RoBorg specified how you actually do that, which I should've.
    – chaos
    Apr 9, 2009 at 15:59
0

You could use something like this to remove all control characters:

RewriteRule ^([^\x00-\x19\x7F]*)[\x00-\x19\x7F]+(.*) /$1$2 [L,R=301]

And for your additional space character:

RewriteRule ^([^\x00-\x20\x7F]*)[\x00-\x20\x7F]+(.*) /$1$2 [L,R=301]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.