The problem with your .htaccess file is that for every space in the title, it will do one redirect. It might or might not be fixed by using a permanent redirect (301
), but even then browser will give an error (redirect loop detected) if too many spaces appear in the title. You can fix both problems by simply doing it all on the server:
RewriteRule ^article/([^\ ]*)\ ([^\ ]*\ .*) /article/$1-$2 [N]
RewriteRule ^article/([^\ ]*)\ ([^\ ]*)$ /article/$1-$2 [L,R=301]
The first rule matches if at least 2 spaces appear in the url, and will rewrite one of the spaces and order Apache to go through the .htaccess
file again ([N]
). If only one space is left, the second rule will match and, besides rewriting that last space, it will also redirect the user. This will only be one redirect, and hopefully the permanent redirect will cause only the new url to be visible in Google.
If there are more spaces in the url than there are internal recursions allowed by Apache, this will result in an Internal Server error. If you have access to httpd.conf
, you can alter LimitInternalRecursion
to allow more internal recursions. Warning: Set this to a SANE number. If, for some reason you have an endless loop in your RewriteRules and this number is insanely high, you'll lock up your server until it hits this limit. See the documentation.