Forum Moderators: phranque
<?php
$str = " fo o+bar%20fubar ";
$str = preg_replace('/(%20|\s|\+)/', '', $str);
// will print "foobarfubar"
echo $str;
?>
<?php
// get the path part of the request
$str = $_SERVER['REQUEST_URI'];
// remove all the characters you don't want
$str = preg_replace('/(%20|\s|\+)/', '', $str);
// if anything changed
if ($str != $_SERVER['REQUEST_URI']) {
// return a 301
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://example.com." . $str );
}
?>
# Detect and remove the first space from the requested URL-path, then save it and re-start
RewriteCond %{ENV:CleanedURLpath} =""
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^%?#\ ])\%(25)*20([^\ ?#]([?#][^\ ]*)?)\ HTTP/
RewriteRule ^. - [E=CleanedURLpath:%1%3,N]
#
# Detect and remove any subsequent space from the saved partially-corrected URL-path, then re-start
RewriteCond %{ENV:CleanedURLpath} ^([^%?#\ ])\%(25)*20([^\ ?#]([?#].*)?)$
RewriteRule ^. - [E=CleanedURLpath:%1%3,N]
#
# Once we get here, all spaces have been removed from the
# URL-path. Invoke an external redirect if any were removed.
RewriteCond %{ENV:CleanedURLpath} ^(.+)$
RewriteRule ^. http://www.example.com/%1 [R=301,L]