4

I have been googling for a good while and can't figure this one out. It seems simple, and I am sure it is, but I am not a server expert.

My url is: http://www.example.com/blog/?tag=Word1+Word2

Some of the tags are a single word (ex: bicycle), some are longer (ex: Two wheel bicycle).

I need this url to be output as: example.com/blog/tag/word1-word2/

How can I get a rewrite to achieve this?

HBruijn
  • 72,524
  • 21
  • 127
  • 192
TJ Sherrill
  • 141
  • 1
  • 1
  • 4
  • Possible duplicate of [Nginx Rewrite Convert Querystring to Path](http://serverfault.com/questions/488444/nginx-rewrite-convert-querystring-to-path) – Froggiz Dec 18 '15 at 16:57
  • I had looked at that one. The question I come back to is that /blog/ is the root of my blog. Those responses in that other post all have /location/file.asmx. I need the blog to work, and post urls like /blog/post-title/. – TJ Sherrill Dec 18 '15 at 17:54
  • `Some of the tags are a single word, some are longer.` — what does it mean? – Alexey Ten Dec 18 '15 at 18:06

1 Answers1

1

You should send - in your query instead of + to get same format and make an easier request :

location ~ /blog/ {
    if ($args ~* "tag=(.*)") {
        set $w1 $1;
        rewrite .* /blog/tag/$w1/? permanent;
    }
}

Based on the link i posted

? at the end will remove the query string parameters (from rewrite doc )

If you specify a ? at the end of a rewrite then Nginx will drop the original $args (arguments)

another to achieve it is to set args to nothing:

set $args '';
Froggiz
  • 3,013
  • 1
  • 18
  • 30