Milan Milosevic
Milan Milosevic

Reputation: 433

htaaccess rewrite and removing words space

I have problem with rewrite and space betwen words, for example my url looks like this

http://xxxsite.com/search/test-test2/

and i'm rewrite this with

RewriteRule ^search/([A-Za-z0-9-\s]+)/$ index.php?mode=search&q=$1&type=text [L]

but now i can see in google webmaster tool some duplicated url's as problem

http://xxxsite.com/search/test-test2/
http://xxxsite.com/search/test test2/

So as you can see 2nd url is problem, i need to remove/replace space with - as example above

I'm try redirect url with space to new one

RedirectMatch 301 ^/search/(.*)\s(.*)/$ http://xxxsite.com/search/$1-$2/  

but this for some reason not working..

There is any chance to remove space and replace it

Upvotes: 0

Views: 153

Answers (1)

anubhava
anubhava

Reputation: 785126

You can use this rule to recursively replace all spaces by hyphens:

RewriteRule "^([^ ]*) +([^ ]* .*)$" /$1-$2 [L]
RewriteRule "^([^ ]*) ([^ ]*)$" /$1-$2 [L,R=301]

Upvotes: 1

Related Questions