htaccess delete spaces on URL rewriting

时间:2019-04-23 15:12:06

标签: .htaccess

Hy, I have this rewriteRule to change a /search?key=ok to /search/ok from a search bar

RewriteCond %{QUERY_STRING} key=([0-9a-zA-Z_-]+) [NC]
RewriteRule (.*) /search/%1? [R=302,L]

But when I search something with spaces like ok ko it's just return /search/ok. What do I have to change to have /search/ok-ko ?

Thanks !

1 个答案:

答案 0 :(得分:1)

您的正则表达式模式([0-9a-zA-Z_-]+)仅匹配字母数字字符,您可以在模式中添加\s以匹配Querystring中的空格字符

 RewriteCond %{QUERY_STRING} key=([0-9a-zA-Z\s_-]+) [NC]
RewriteRule (.*) /search/%1? [R=302,L]