4

I want to do case insensitive URL redirection in nginx. Below is my code.

location ~* WapsiteDataFetch{
      rewrite  WapsiteDataFetch(.*) http://images.xample.com/xyz/images$1 permanent;
    }

In the above case,

www.example.com/WapsiteDataFetch is redirected properly to http://images.xample.com/xyz/images. However, the URL "www.example.com/WAPSITEDATAFETCH" is not redirected properly.

Even if I change a single character, it is giving a 404 error.

I have tried many blog posts and have seen many post from Stack Overflow and many of them have suggested "~*", but in my case it was not helping me.

How can I fix this?

1

2 Answers 2

22

Use (?i) to match case-insensitively - http://perldoc.perl.org/perlretut.html

Location block is not necessary. Try this.

rewrite (?i)^/WapsiteDataFetch(.*) http://images.xample.com/xyz/images$1 permanent;
7
  • What do I do if there are queries in front of the url such as www.example.com/WapsiteDataFetch?q=123 etc..How should i implement this Commented Feb 5, 2014 at 15:13
  • 1
    Remove the ? after /images$1 to have request args appended. Commented Feb 5, 2014 at 15:18
  • It did not work..Any other suggestion you can give me. Commented Feb 6, 2014 at 5:56
  • What is the use of proxy in the nginx.conf file Commented Feb 6, 2014 at 6:04
  • I've updated the code. Does it work for you? Proxy allows passing requests to another server. - nginx.org/en/docs/http/ngx_http_proxy_module.html Commented Feb 6, 2014 at 6:09
2

You can avoid using the regular expression engine twice, by doing the capturing inside the location block:

location ~* WapsiteDataFetch(.*) {
  return 301 http://images.xample.com/xyz/images$1;
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.