Redirection and replacement with Nginx - redirect

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

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.

how to create website redirect with a # in the link

I need to create a redirect in the Serverblock on my nginx Server.
I want to use the same code which is already working for other redirects:
http://www.domain.com/text
location /text {
rewrite ^/.* http://www.domain.com/target permanent;
}
this works fine!
But this code is not working with a specific link
http://www.domain.com/#text/text
location /#text/text {
rewrite ^/.* http://www.domain.com/target permanent;
}
I believe it is because of the # sign but my problem is I need the exact URL name with the # sign. Is there a way to uncomment a # sign or do I need to go a complete different way?

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

Nginx rewrite subfolder to subfolder

I have been having issues redirecting an old subfolder entirely to a new subfolder. I need to have both /old and /old/ go to /new and /new/ respectively
I also need to have any parameters followed after /old/blah/blah2/ to /new/blah/blah2/ so just basically replacing old with new no matter whats called. The below is the closest I can get.
location /account/ {
rewrite ^/account/(.*)$ https://$server_name/portal/$1 permanent;
}
Example of an actual URL:
www.domain.com/account/index.php?loend=true&cmd=callback&module=8
needs to be
www.domain.com/portal/index.php?loend=true&cmd=callback&module=8
Thank you
Can you try rewrite ^/account/(.*)$ /portal/$1; ? No need to put it into location /account/ { ... } – Guillaume Filion Jul 24 at 19:00
Using just: rewrite ^/account/(.*)$ /portal/$1;
without specifying the location has resolved all issues
Its should work but you're missing $ sign in it.
Here is corrected a bit code
rewrite ^/account/(.*)$ https://$server_name/portal$1 redirect;
or
rewrite ^/account/(.*)$ https://$server_name/portal$1 last;
or
rewrite ^/account/(.*)$ https://$server_name/portal$1;
Than reload config of nginx
service nginx reload
here is source site.
https://www.digitalocean.com/community/questions/301-redirect-nginx
Since this seems more of a redirect than a rewrite, I would use return
location ^~ /account(.*) {
return 301 https://$server_name/portal$1$is_args$query_string;
}
The $is_args$query_string is to append any query string like you mentioned in one of the comments loend=true&cmd=callback&module=8 and also mind that if the $server_name is the same name of server_name you can replace it with $http_host and keep it dynamic.

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.