Ahmed
Ahmed

Reputation: 57

replace %20 with hyphen in htaccess

The following code to replace %20 with hyphen is not working perfectly. Some times it works and sometimes it doesn't and if there is a numeric in the title, the spaces are not replaced after the numeric. Any help? For eg.
he is 30 years old
he-is-30%20years%20old

RewriteEngine On
RewriteCond %{THE_REQUEST} (\s|%20)
RewriteRule ^([^\s%20]+)(?:\s|%20)+([^\s%20]+)((?:\s|%20)+.*)$ $1-$2$3 [N,DPI]
RewriteRule ^([^\s%20]+)(?:\s|%20)+(.*)$ /$1-$2 [L,R=301,DPI]

Upvotes: 3

Views: 1506

Answers (1)

anubhava
anubhava

Reputation: 785128

Instead of those complex replacements try these 2 rules to replace all space by hyphens:

RewriteEngine On

RewriteRule "^(\S*)\s+(\S*)$" /$1-$2 [L,NE,R=302]

RewriteRule "^(\S*)\s+(\S*\s+.*)$" $1-$2 [L]

# remove multiple hyphens
RewriteRule ^(.*)-{2,}(.*)$ /$1-$2 [L,R=302]

Upvotes: 3

Related Questions