0

Suppose I have a URL http://www.example.com/category/Travel%20Accessories which is now changed to http://www.example.com/category/TravelAccessories (%20 is removed) can some one tell how using nginx I can redirect request for http://www.example.com/category/Travel%20Accessories to http://www.example.com/category/TravelAccessories . Please note Travel%20Accessories is one example there many other such url where that space(%20) is removed

Regards Rishi

Rishi Saraf
  • 1,644
  • 2
  • 14
  • 27
  • 1
    take a look at this SO answer to a related question http://stackoverflow.com/questions/15912191/how-to-replace-underscore-to-dash-with-nginx?answertab=active#tab-top. It is quite thorough and I think you can apply to your use case. – steve klein Jul 07 '15 at 15:42

1 Answers1

0

You can not remove an arbitrary number of spaces. The problem is that for a rewrite you have to specify a regular expression to match the whole URL and a replacement expression for the whole URL.

This rewrite rule should replace one space in a URL beginning with /category/:

rewrite ^(/category/.*)\ (.*)$ $1$2 permanent;

You could add more spaces to be captured like so:

rewrite ^(/category/.*)\ (.*)\ (.*)$ $1$2$3 permanent;
rewrite ^(/category/.*)\ (.*)$ $1$2 permanent;

Note that you need to have one rule for each possible number of spaces (1 and 2 in this case).

Lukas Boersma
  • 1,022
  • 8
  • 26
  • Thanks Lukas it did worked with small change i.e rewrite ^(/category/.*)\ (.*)$ $1$2 permanent; . I had to replace %20 with \ (\space) . And for more space no need add anything extra. My rewrite rule took care of all the spaces. – Rishi Saraf Jul 07 '15 at 16:48