Page is a not externally linkable
- Code, Content, and Presentation
-- Apache Web Server
---- Removing spaces from urls


jdMorgan - 1:26 am on Nov 18, 2010 (gmt 0)


Yes, perhaps the best you can do using RewriteRules is to use the RewriteRule [Next] function in a three-rule set:

# 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]

The allowance for "25" preceding "20" is to handle multiply-encoded spaces.

This is a fairly expensive rule-set; If using a more-specific pattern in the RewriteConds and RewriteRules is possible based on the "types" of the URLs that are being mis-linked and the nature of the linking errors, then I would recommend doing so.

Jim


Thread source:: http://www.webmasterworld.com/apache/4228596.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com