how to make this nginx location redirect not case sensitive - redirect

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.

Related

Nginx: one directory should be redirected below itself

I have been struggling with a simple problem with nginx redirecting, but to avail so far.
I have two (language based) domains:
server {
server_name a.b.com;
.
.
.
}
server {
server_name d.b.com;
.
.
.
}
Now on d.b.com/example, should redirect to d.b.com/example/right. I have tried to build a location like below (plus a lot of variations of it including full urls).
location ^/example {
redirect /example /example/right permanent;
}
Nginx allows the syntax, but when I try to open the page, I always get the 404.
I think this should be straight-forward but my brain just can't get it right, nor have I found the solution in the web.
The biggest problem is that the end users already sent the mass mailing to our customers with the invalid link (without even checking it!), so I am a bit hard pressed with timetable...
wbr
hank
If you are trying to match a single URI, use an exact match location:
location = /example {
return 301 /example/right;
}
See this document for details.

Nginx redirect some sub folders

Can anyone point me in the right direction. I would like to redirect some links but not the root directory. For example:
example.com/post-on-something
example.com/post-on-something-else
redirects to the new domain:
newdomain.com/post-on-something
newdomain.com/post-on-something-else
however I would like
example.com to show the new website (which is a single page)
Because I have a large number of posts I don't know what each page will be and I would like to avoid a rewrite rule for every old post as this would hit well over 100.
So far I have:
location / {
return 302 https://www.newdomain.com/$request_uri;
}
But this does everything.
Thanks for your help,
Tom.
You're nearly there... just need to use the syntax for an exact match
location = / {
root /data/www;
}
location / {
return 302 https://www.newdomain.com/$request_uri;
}

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 ;
}

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.

How to set up nested subfolder to subdomain Nginx 301 redirects

I'm trying to set up Nginx redirects that will rewrite any URL from several older forum setups to a new forum. The older forums ran from subfolders, while the current forum is running from a subdomain of the same site.
So, for example, I want ANY request to site.com/ask to be redirected to the front page of forum.site.com. Since I'm dealing with 3 old forums, I tried to set up a nested redirect like this:
location ~ ^/\~([^/]+)/(.*)$ {
location ~ ^/\~ask/(.*)$ {
rewrite ^(.*)$ http://forum.site.com$1 permanent;
}
location ~ ^/\~forum/(.*)$ {
rewrite ^(.*)$ http://forum.site.com$1 permanent;
}
location ~ ^/\~qa/(.*)$ {
rewrite ^(.*)$ http://forum.site.com$1 permanent;
}
}
With the above rules, only the first one works and partially. For example, a request to site.com/ask gets redirected to forum.site.com, which is fine, but any request to, say, site.com/ask/what-is-this goes to forum.site.com/404.
Request to site.com/forum and site.com/qa do not work at all.
I'm sure there's a simpler way of doing this, but I don't want to spend several days trying to figure it out.
Your input is welcome and appreciated.
Edit:
Not getting anywhere with the above code, I reduced it to this:
location ~ ^/\~([^/]+)/(.*)$ {
location ~ ^/\~(qa|forum|ask)/(.*)$ {
rewrite ^/~(qa|forum|ask)/(.*)$ http://forum.site.com$1 permanent;
}
}
But the result is still the same. Any ideas?
Actually you want to redirect user to the main page of your forum regardless of what page he came to. Try this.
location /qa/ {
return 301 http://forum.site.com;
}
location /ask/ {
return 301 http://forum.site.com;
}
location /forum/ {
return 301 http://forum.site.com;
}