Haproxy, how to use case insensitive redirects map? - haproxy

So, I want redirects like these to work:
example.com/QWER -> example.com/qwer
example.com/Qwer -> example.com/qwer
example.com/QwEr -> example.com/qwer
What I have tried:
http-request redirect location %[path,lower,map_str(/usr/local/etc/haproxy/website-redirects.map)] code 301 if { path,lower,map_str(/usr/local/etc/haproxy/website-redirects.map) }
And different combinations: with "map_reg", without "lower", using -i etc.
Thanks in advance!

Related

how to make this nginx location redirect not case sensitive

my situation:
domain.com is a WordPress site (and WP works fine). its installed at the root.
I also created/add a sub folder: domain.com/subfolder
that has an index.php page in it, like so:
domain.com/subfolder/index.php
I need any URL entered into browser to redirect to subfolder index.php page.
I have this nginx location, but it ONLY works if you type in case sensitive subfolder or subfolder/, I need non-sensitive:
location /subfolder {
return 301 http://domain.com/subfolder/index.php;
}
location /subfolder/ {
return 301 http://domain.com/subfolder/index.php;
}
SO basically, if user types anything except subfolder or subfolder/, let WP take care of the url. otherwise, redirect it.
I've tried ~ ^ *, but can't get it to work right.
what am i doing wrong? :)
thanks!
PS. if you see something else that I should be doing, please let me know -- i'm new to nginx
Try this:
location ~* /subfolder/ {
return 301 http://domain.com/subfolder/index.php;
}
The tilde and asterisks ensure that this location will be matched case insensitive.

Can not use 301 redirect in NGINX

I have a web site which is translated to 18 languages. Some languages are the same (Brazilian is Portuguese). So I want to redirect from br to pt to avoid odd content, from /some_domain/br/... -> /some_domain/pt/...
I can write single redirect from one domain to another. Something like this:
location = /user/unique {
return 301 http://www.usgreencardoffice.com/blog/the-american-dream;
}
I want to achieve the following:
domain.com/br/something -> domain.com/pt/something
But for the languages redirection, I have no idea. How can I achieve this?
If the language code is at the beginning of the URI, a prefix location will be an efficient solution:
location ^~ /br/ {
rewrite ^/br(.*)$ /pt$1 permanent;
}
The ^~ modifier makes this prefix location take precedence over regex locations at the same level. If you change permanent to last, the rewrite becomes internal and thus invisible to the user.
See this and this for details.

Nginx redirect all contents of a subdirectory to another subdirectory

Let say that I have a url like this:
http://www.example.com/admin/admin.php?fail=1
how can I rewrite the url to be
http://www.example.com/another/subdirectory/admin.php?fail=1
Thank you
Update: this is what I've tried so far, but it will not redirect admin.php?fail=1
location /admin/ {
rewrite ^/admin/(.*)$
/another/subdirectory/$1 redirect;
}
I rather use return 301 for redirections and use rewrite only if I want to display something like a nice url.
Please try the following
location ~ ^/admin/(.*) {
return 301 /another/subdirectory/$1 ;
}

Redirection and replacement with Nginx

I am having trouble getting this to work in nginx:
I am trying to rewrite, a url string like this to this:
http://domainname/n/xxx = > http://domaindomain/x/xxx
I would like only "n" to change to "x"
I have tried :
location /n/ {
rewrite ^/n/(.*)$ /x/? last;
}
Thanks for the help
You need to use the following rule:
rewrite ^/n/(.*)$ /x/$1 permanent;
(.*) will capture your URL after the /n/ part and $1 will place the same thing in the target URL.
Using the permanent keyword will redirect the users with a 301 HTTP status code

Redirecting and rewriting in NGINX

I'm trying to create a simple "Hello World"-like API and for that I need a rule to redirect/rewrite the URL to my API.
Let's say my file is called index.php, so whenever I make a GET to index.php I get a list of items.
The first thing I want to do is to redirect the URL mydomain.com/index.php to mydomain.com/api.
And second, when mydomain.com/api is accessed, I'd like the server to trigger the index.php file without rewriting the URL.
My current code looks like this:
location /api {
rewrite ^ $scheme://$host/index.php permanent;
}
location /index.php{
return 302 www.mydomain.com/api;
}
but it's not working as expected. Why and how can I fix it?
You need two rules for what you're trying to achieve.
The first one, the one that will receive requests and "translate" them to your under-the-hood script, should look like this:
rewrite ^/api\?(.+)$ /index.php?$1 last;
As for the second one, the one that should redirect all your users to the "beautiful" URL:
rewrite ^/index.php\?(.*)$ /api?$1 permanent;
Note that this second rule should be outside any location block and before any of those, as you're willing to redirect the user before anything else.
Cheers
# you don't need a rewrite. Use location with the "=" or exact match
location = /api {
alias /path/to/root;
index index.php;
}
location /index.php {
return 302 www.mydomain.com/api;
}
Hope it helps
Here is the second version of my answer using one redirect and an alias:
location /api {
alias /path/to/root;
index index.php;
}
# replace index.php with api
location /index.php {
rewrite (index\.php)(.*)$ /api$2 permanent;
}
My first solution did not forwarded the args. Reading #alexandernst solution gave a better idea of the problem.