I have rewritten all external links sent to standard output on the fly as an internal link in PHP from https://externalsite.com to https://myexamplesite.com/https://externalsite.com, yet I want Nginx to handle the request for this URL and not PHP.
I have tried it via the line below, but it is not working
rewrite ^/http(.+)$ http$1 redirect;
Please advise on the best way to redirect https://myexamplesite.com/https://externalsite.com, to https://externalsite.com on request
I was able to have it work with this.
rewrite ^/https?\W+(.*) https://$1 redirect;
I am not sure why the former did not work but seems NGINX is passing it back internally.
NGINX seems not to honor an external redirect without the "https://" statically written before and jointly with the second option of the directive despite being captured in the parenthesis.
Related
I have
many pages which were indexed by search engines with crappy GET-parameter like _escaped_fragment_ (for more info about escaped fragments see more Yandex man page)
nginx as reverse proxy in front of many different frontend apps
So I need to get 301 redirect only for all these pages with some GET-parameter to the same pages but without any get parameters. For example
example.com/some/long/path?_escaped_fragment_=
should be 301-redirected to
example.com/some/long/path
I can do it by adding this logic to each frontend app or I can do it at nginx configuration. I prefer to use second variant.
Potential solution can include
using http rewrite module
using if in rewrite to check GET-params
using map module to define request uri path without args from $request_uri
I've fixed my initial issue in such manner
add below code to my http context to define request uri path without args from $request_uri (here we use map module)
map $request_uri $request_uri_path {
"~^(?P<path>[^?]*)(\?.*)?$" $path;
}
add below code to my server context to make 301 redirect from pages with _escaped_fragment_ in GET parameters to the same pages without any GET parameters (here we use rewrite module)
if ($args ~* "_escaped_fragment_") {
rewrite ^ $scheme://$host$request_uri_path? permanent;
}
And this works for me.
I have a rewrite rule that looks like the following
rewrite ^/([a-zA-Z0-9_]+)$ /mysite/#!/$1/login;
The idea is that a shortcode like
/foo
gets redirected to
/mysite/#!/foo/login
However nginx is redirecting to:
/mysite/%23!/foo/login
How do I prevent the URL encoding from happening in the rewrite?
I can reproduce this issue by using a reverse proxy.
Nginx is actually doing the right thing, as # is a reserved character for URIs and identifies the start of the fragment identifier.
The fragment identifier is for the browser's use only and is not usually received by the server in the requested URL. I am not sure how your Tomcat server is receiving requests containing a naked # in the first place.
E.g.
rewrite ^/page /#page;
I'm not sure if this would even be possible, due to the nature of fragments (never sent to the server etc). But since I'm rewriting TO one, rather than FROM one, I think it should work?
In which case how do I encode/escape that hash sign so that it doesn't start a comment..?
Thanks!
As you know, the fragment is used by the browser and is not sent to the server.
But you can use nginx to rewrite a request into a new URI containing a fragment, but it only makes sense if the new URI is sent to the browser, i.e by using an HTTP 3xx response.
The rewrite directive will generate an HTTP 3xx response when the redirect (302) or permanent (301) flag is provided (or the replacement string starts with a scheme - see this document for more).
For example:
rewrite ^/page /#page redirect;
Adding quotation marks, could help. I tried in Docker Nginx latest image (1.17.4)
rewrite ^/page "/#page" redirect;
I'm trying to rewrite a url like the following but without sending a 301 to the client (change the url and then restart processing of the server block with the new url).
rewrite ^/a/b/$ /a/c/d permanent;
I would normally just reorder the lines but it's a really convoluted config and also really large.
permanent always means redirect, replace it with last
check the last flag in the reference page
We just moved to a new site, and want to redirect old links where necessary - however, some still work. For instance,
/holidays/sku.html
still works, while
/holidays/christmas/
no longer works. I'd like to be able to allow the site to attempt to serve a page, and when a 404 is reached, THEN try to pass it through a series of regex redirects, that may look like:
location ~* /holidays/(.*)+$ { set $args ""; rewrite ^ /holidays.html?r=1 redirect; }
I'm using a ~* location directive instead of doing a direct rewrite because we're moving from a Windows-based ASPX site to Magento with php-fpm behind nginx, so we suddenly have to worry about case sensitivity.
Without using nested location directives (which are actively discouraged by nginx documentation) with an #handler of some sort, what's the best way to allow nginx to attempt to serve the page first, THEN pass it across redirects if it fails?
Thanks!
http://wiki.nginx.org/NginxHttpCoreModule#try_files