Forum Moderators: open
Does anybody know a valid RewriteRule for Apache's mod_rewrite that strips out whitespaces from URL's? The whitespace always follows a /, so there is a nice pattern to match. I tried some rules myself, but after repeatedly being forced to kill those threads, I've given up :(
RewriteRule "([^ ]*) +(.*)" $1$2 [N,E=AC_REWRITE:true]
RewriteCond %{ENV:AC_REWRITE} true
RewriteRule (.*) [%{HTTP_HOST}$1...] [R=permanent,L]
First RewriteRule
Match as many characters that are not spaces (([^ ]*) - store them in $1) followed by one or more space (+) followed by as many characters as possible ((.*) - and store them in $2). If the rule matches we rewrite to $1$2 which contains the URL without the first set of spaces and store true in the environment variable AC_REWRITE. This process is repeated until the rule no longer matches (flag N).
RewriteCond
Look up the environment variable. Rewrite only if following RewriteRule matches and AC_REWRITE is true.
First RewriteRule
Force an external redirect in case we removed any spaces.
This rule set should be at the beginning of your rewriting process since the flag N will restart rewriting with the first rewrite rule.
if (strstr(rawurldecode($_SERVER['REQUEST_URI']),"/ ")) {
header("Location: http://" . $_SERVER['SERVER_NAME'] . str_replace("/ ", "/", rawurldecode($_SERVER['REQUEST_URI'])));
exit;
}
in my 404.php. This works fine. The only (very minor) disadvantage is that each of these hits is written to Apache's errorlog.
Haven't tried andreas' rules yet, will do very soon.
RewriteRule ^(.*)/\ (.*)$ $1/$2 [N,R=permanent]
While this approach is easier, just one rule it will cause an external redirect, i.e. extra traffic after each substitution. My rules strip off all whitespace internally and then do one external redirect to get the URL right in the userīs browser.
whereas andreas' rules seem good for .htaccess.
Actually I tested my rules only in httpd.conf and not in a .htaccess file although they should work there just as well.
Andreas
I have a simple domain.com to www.domain.com rule in affect on one of my sites and right now(time of day, alignment of planets,etc) it's redirecting properly, in IE and Opera. Last night, it didn't (different pc, but shouldn't matter?).
If you did an external rewrite
Thatīs what the [R=permanent,L] does in the second RewriteRule in my post #2 [webmasterworld.com].
Andreas