Home > nginx > Nginx redirect all requests trying to reach a folder

Nginx redirect all requests trying to reach a folder

December 30Hits:5
Advertisement

How can I redirect all requests trying to reach a folder?

for example, I would like to redirect:

somedomain.com/folder/subfolder/index.html somedomain.com/folder/subfolder2/something.html somedomain.com/folder/subfolder3/somethingelse.html 

to

somedomain2.com/index.html 

what I've tried:

if ( $request_uri = "/folder/.*" ) {   rewrite ^/(.*)$  http://domain2.com/embed.html    permanent; } 

Answers

server {
    server_name somedomain.com;

    location /folder/ {
        return 301 $scheme://somedomain2.com;
    }
}

server {
    server_name somedomain2.com;

    location / {
        #index index.html; # You could wish to add that, if index default value does not suits your needs
    }
}

That will issue a permanent redirect for any /folder/* request.

I took the liberty of mimicking the scheme being used with somedomain for somedomain2 with the $scheme variable.

I have not forced the /index.html URI in the redirect. You may wish to do it. I also relied on the default value of the index directive, which is maybe not right for you.

If you wished to handle the content from the other server transparently, you could use proxy_pass to proxy the request to it.

Tags:nginx, rewrite

Related Articles

  • Nginx redirect all requests trying to reach a folderDecember 30

    How can I redirect all requests trying to reach a folder? for example, I would like to redirect: somedomain.com/folder/subfolder/index.html somedomain.com/folder/subfolder2/something.html somedomain.com/folder/subfolder3/somethingelse.html to somedom

  • How do I make Nginx redirect all requests for files which do not exist to a single php file?January 5

    I have the following nginx vhost config: server { listen 80 default_server; access_log /path/to/site/dir/logs/access.log; error_log /path/to/site/dir/logs/error.log; root /path/to/site/dir/webroot; index index.php index.html; try_files $uri /index.ph

  • nginx: Redirect all requests to a proxy, but not these three specific URLsAugust 4

    nginx can be tricky sometimes: I want to redirect all incoming requests for www.sub.domain.com/* to www.other.domain.com/* use a proxy so users still see the original www.sub.domain.com when browsing the site add an exception for specific URLs (all t

  • Nginx - redirecting all requests after a URL path to a PHP scriptJanuary 14

    In our existing Apache setup, we have a .htaccess file in a folder which redirects all requests to a PHP handler. For example: www.example.com/gallery/user01/xyz.png For this URL request: The "/user01" part symlinks to a "/imageDB" loc

  • Nginx redirecting POST request with proxy_pass can't resolve urlJanuary 8

    I'm trying to redirect POST requests with a proxy_pass. Here is my full servers config (2 servers): # default server with all my subdomains server { listen 80 default_server; listen [::]:80 default_server; server_name *.example.com; passenger_enabled

  • Nginx redirect all request that does not match a file to a php fileNovember 6

    I'm trying to get all request to: http://example.com/downloads/* redirect to http://example.com/downloads/index.php except if the requested file exist in /downloads/ ex: http://example.com/downloads => /downloads/index.php http://example.com/download

  • Nginx redirects http request to one vhostApril 22

    I have installed nginx with php-fpm. When I try to access example.org it redirects me to https://example.com What's wrong with my configs? nginx.conf user www; worker_processes 1; events { worker_connections 1024; } http { include mime.types; default

  • nginx: redirect all requests from one domain to other with a certain scriptNovember 19

    So what I have: nginx php-fpm What I need: Main domain: example.com Other domain: otherexample.com In main domain we have script, which can do some logic and need when we request to otherexample.com do request to example.com/somescript.php?domain=oth

  • Why is nginx redirecting requests on port 8080 to port 80(apache)February 19

    I have a wordpress blog setup on apache2 (port 80) on Ubuntu 12.04, I also have a nginx setup listening on port 8080 serving the same blog. My problem is, whenever I try to open the blog on port 8080, nginx redirects the requests to apache which shou

  • How to redirect secure requests to another subdomain in nginxApril 9

    I've been searching pretty long for this one, but nothing I seem to read works right -- maybe I'm not searching for the right things. Anyway, we have a www.example.com and a secure.example.com. I'm able to successfully redirect insecure requests to S

  • How to configure nginx to redirect all request from domain aliases to main domain?November 8

    I do have an nginx server which responds to several domains and I do want to redirect all request to the main domain. Example: website responding for xxx xxx.example.com yyy.example.com $hostname for both http and https. I want to configure the serve

  • How to redirect http requests to https (nginx)October 22

    There appear to be many questions and guides out there that instruct how to setup nginx to redirect http requests to https. Many are outdated, or just flat out wrong. # MANAGED BY PUPPET upstream gitlab { server unix:/home/git/gitlab/tmp/sockets/gitl

  • Change Nginx redirection rules based on content-type of requestAugust 6

    I'm having a reverse-proxied Nginx server. I just wanted to know if it is possible to redirect the request in Nginx based on the content-type of the request? Right now, I'm checking the URL for a keyword, and based on that, I redirect the request. So

  • Nginx: Redirect all www traffice to non-www versionFebruary 21

    How do I redirect an request at www. to the non-www version? This should include requests to the root domain as well as any (wildcard) subdomain. I also don't want any unnecessary if statements, as per the nginx pitfalls document. Here's what I have

  • NGINX Redirect Rule: Subfolder only to HTTPS?April 28

    I'm trying to redirect anyone who access the admin section of my site to the HTTPS version of it. The current rewrite rule is as follows: server { listen 80; server_name domain.com; location / { index index.php index.html; root /home/domain.com/publi

  • IIS - HTTP Redirect all requests for one virtual directory to anotherNovember 20

    How do I set up an HTTP Redirect rule to redirect all requests for a virtual directory to another virtual directory, when I don't know the hostname or complete URL, and cannot use the URL Rewrite module? The following redirects should work: http://ho

  • Nginx redirect folder to subdomainJune 5

    I'm trying to configure a redirect on Nginx from a folder to a subdomain. I've tried already Nginx redirect: folder to external domain and Nginx rewrite rule (subdirectory to subdomain) and nginx: redirect subfolder to subdomain. Nothing works. The p

  • How to configure Apache web server to redirect the requests it receives without modifying the source IP?June 11

    Unfortunately I don't know about web servers but I'm trying to help a colleague. We have an Apache web server setup and we need it to redirect resend the requests it gets to an application server while keeping the source ip the same it was when apach

  • nginx redirect all http to https with one exceptionJuly 17

    I would like to redirect all HTTP traffic to HTTPS, with one exception. Anything with /preview/ in the URL should be served via HTTP. I have tried with below configuration, but it keeps telling me I have a redirect loop. server { listen 80; server_na

  • Nginx: redirection to (private ip) virtual machinesJuly 29

    I want to setup a set of web services for my colleagues from my laboratory; simple things like wiki, cms, git, etc, so that we can document our projects better. I'm an physicist, not CS, so sorry if I miss something obvious. The services would be beh

Copyright (C) 2017 ceus-now.com, All Rights Reserved. webmaster#ceus-now.com 14 q. 0.470 s.