I have tried many options, but did not find a suitable answer. I can do this in Apache, but I can’t figure out how to do a redirect in Nginx while maintaining the URL
.../s1 changes to .../image.jpg - how to fix it?
location /s {
rewrite "/s1" https://example.com/image.jpg last;
}
A redirect always changes the url, a rewrite keeps it internally.
In this case nginx is turning your rewrite into a redirect, because you specified a different server. You have 2 options:
If the url you want to rewrite to is local, you should simply remove the https://example.com part to make the url relative instead of absolute.
If you really want to mask a different server, then what you need to build is a reverse proxy. Nginx does have features for this.
Related
I am trying to redirect an app to some static html files.
I am using NGINX as a reverse proxy, and alias to use a specific path.
So I am using a simple Redirect within a react container:
redirectTo="/documentation/index-docs.html"
This is the Nginx config part used for that:
location /documentation {
alias /workspace/documentation;
rewrite ^(.+)/+$ $1 permanent;
The current behavior is that it keeps looping and adding tailing slashes.
http://localhost:3001/documentation/index-docs.html/////////////
Did anyone encounter the same issue before ?
Thanks.
So we ran into an SEO issue with a WPML after changing from using URL parameters to specify the site language to using the path. The web server is running nginx.
Before:
example.com/?lang=fr
example.com/example-path/?lang=fr
After:
example.com/fr/
example.com/fr/example-path/
So what I'm trying to do is redirect any URL following the old URL format, including the root / to the new format. This process should strip all URI parameters in the URL and replace it with the corresponding path.
I reached out to the guys over at WPML and they don't know how to do it.
I've tried two different ways:
location = / {
if ($args ~ "^lang=(fr)") {
set $key1 $1;
rewrite ^.*$ /fr last;
}
}
As well as:
rewrite ^/.*\?lang\=fr$ /index.php? permanent;
But unfortunately from what I can tell, neither of these seem to do anything. I'm really familiar with regex but for some reason I'm having a hard time with these nginx rewrites.
I was surprised that there are very few examples in the nginx docs and on google about rewriting URL parameters. Any ideas on how this could be done?
Thanks!
This nginx rule works great for me for a full specified file path
rewrite ^/sitemap.xml$ /sitemap.php last;
When I acces sitemap.xml it works as expected but in the background sitemap.php is requested. So far so goode.
Another problem arised and I need to rewrite the last part of existing urls
rewrite ^doctor-solution.html/ doctor-answer.html/ permanent;
What I want to achive is when an old url like
https://example.com/case12232-doctor-solution.html/ is accessed
it must be redirected to
https://example.com/case12232-doctor-answer.html/
But My rule doesn't seem to work. Any ideas?
I am using nginx as a reverse proxy, a Angular Universal application is serving in the background.
I am trying to rewrite an old URL pattern to a new one via nginx, namely
server {
rewrite ^/s/(.+)$ /search\;q=$1 permanent;`
...
}
such that something like /s/keywords gets redirected to /search;q=keywords
Unfortunately, the above nginx rule turns
/s/keywords into /search/;q=keywords (so a slash gets added after /search). Is there a chance to remove this slash so the result is /search;q=keywords?
(The application running behind nginx expects the url to be /search;q=keywords.)
You could add quotes to the replacement, for example:
rewrite ^/s/(.+)$ "/search;q=$1" permanent;
I recently moved a subdomain to my main domain but I also changed the url structure.
Previously I had pages like http://sub.domain.com/companies/my-company-id/year/2012/charts
When moving to the main domain, I removed all the complicated urls to juts get:
http://www.domain.com/companies/my-company
I currently have the following rule:
rewrite ^/companies/(.*)$ http://www.domain.com/companies/$1 permanent; but when someone go on a page like http://sub.domain.com/companies/my-company/2012/charts they get redirect to http://www,.domain.com/companies/my-company/2012/charts and get a 404.
I like to force a redirection to http://www,.domain.com/companies/my-company-id regardless of what's after the my-company-id
Currently the parameter $1 is having the entire URI after /companies, so you are getting redirected to the original path. You should only extract the company-id in $1.
Use this:
rewrite ^/companies/(.*)/(.*)$ http://www.domain.com/companies/$1 permanent;
Here the rest of the URI after company-id will be available in the parameter $2, which is not needed in the rewrite condition.