RewriteRule: Replace spaces along with underscores

I want to modify my .htaccess file so that URL’s default to lower case, at the same time replace underscores and/or spaces with dashes. So the URL MySite/World/Isle of_Man should default to mysite/world/isle-of-man

I modified my httpd.conf file so it defaults ULR’s to lower case when the first line in the code below is added to my .htaccess file.


RewriteRule (.*?[A-Z]+.*) /${tolower:$1} [R]
RewriteRule ^([^_]*)_([^_]*_.*) $1-$2 [N]
RewriteRule ^([^_]*)_([^_]*)$ /$1-$2 [L,R=301]

The next two lines appear to handle the underscore replacement. Isle_of_Man now defaults to isle-of-man as it’s supposed to.

I just wondered if anyone knows how to add empty spaces to the rule, so that isle of man also defaults to isle-of-man.

P.S. There are websites that let you text regex scripts. Are you aware of a similar site that lets you test RewriteRules? When I’m experiment with them, I often crash my computer as my webpages get caught in a loop trying to reload.

Thanks.

chavista,

The empty space (%20 in URLs) can be specified in a character range definition, i.e., [\ ]. Yes, there is a space after the \ and the escaping \ is required because the normal space character is a delimiter between parts of the mod_rewrite code.

'Glad you got the tolower function to work.

Regards,

DK