Need help to rewrite search URL to clean

PCTag

Junior Member
Joined
Jun 25, 2011
Messages
159
Reaction score
21
Hi,

I would like to rewrite the following Apache rewrite rule to nginx. But the nginx test failed. What is the wrong with this code?

What is want to replace the WordPress default search URL slug (/?s=) to clean URL (like this /search/).

# Change WordPress search URL
RewriteCond %{QUERY_STRING} \\?s=([^&]+) [NC]
RewriteRule ^$ /search/%1/? [NC,R,L]

I have converted this apache to nginx using this tool. I have added it to the server { block. But when I try to reload nginx it shows a duplication location.

location / {
if ($query_string ~* "\\?s=([^&]+)"){
rewrite ^/$ /search/%1/? redirect;
}
}

This is the nginx error.

root@server:~# sudo nginx -t && sudo service nginx restart
nginx: [emerg] duplicate location "/" in /var/www/domain.com/conf/nginx/nginx-custom.conf:3
nginx: configuration file /etc/nginx/nginx.conf test failed


Then I slightly change it like the following two situations, but it is not working. Any help would be appreciated.

location ~ / {
if ($query_string ~* "\\?s=([^&]+)"){
rewrite ^/$ /search/%1/? redirect;
}
}

location ~* \ {
if ($query_string ~* "\\?s=([^&]+)"){
rewrite ^/$ /search/%1/? redirect;
}
}

location ~ \ {
if ($query_string ~* "\\?s=([^&]+)"){
rewrite ^/$ /search/%1/? redirect;
}
}

if ($query_string ~ "\\?s=([^&]+)") {
rewrite ^/$ /search/%1/? last;
}
 
Back
Top