Currently I have something like this in my nginx.conf file:
location ~ /old/page/?$ {
return 301 /new-page;
}
The issue is that query strings are being stripped from the /old/page?ref=xx URL.
Is it possible to include query strings using the redirect method I'm using above?
Anything from the ? and after is the query string and is not part of the normalised URI used in location and rewrite directives. See this document for details.
If you want to keep the query string, either add it to the return:
location = /old/page/ {
return 301 /new/page$is_args$args;
}
Or with rewrite, the query string is automatically appended unless a ? is added:
rewrite ^/old/page/$ /new/page permanent;
See this document for location syntax, and this document for return/rewrite.
Related
I had links to my images something like it:
mydomen.com/upload/iblock/f88/f887c7bc3229b93a0a0f7c248d3aefb5.jpg
regex is something like it:
mydomen.com/upload/(catalog|iblock|medialibrary|partners|resize_cache|rk|sale|uf)/*/.(jpg|png|jpeg)
and now i thansfered all my images to storage and now my images have these urls:
images.mydomen.com/iblock/f88/f887c7bc3229b93a0a0f7c248d3aefb5.jpg
so, from mydomen.com i move to images.mydomen.com and removed /upload/
can anyone helps me to redirect all requests images to new domain via nginx location??
You will need to remove the /upload/ prefix from the original URI using a regular expression and capturing the remainder. You can use either the rewrite or location directives.
For example:
rewrite ^/upload((catalog|iblock|medialibrary|partners|resize_cache|rk|sale|uf)/.*\.(jpg|png|jpeg))$ https://images.example.com$1 permanent;
Or using a location block:
location ~* ^/upload((catalog|iblock|medialibrary|partners|resize_cache|rk|sale|uf)/.*\.(jpg|png|jpeg))$ {
return 301 https://images.example.com$1;
}
The regular expression location blocks are evaluated in order, so its position within the configuration file may be significant. See this document for details.
I have a website with pagination, which chooses the page to display using the 'p' query.
My issue is that
www.example.com/category.html and
www.example.com/category.html?p=1
Are the same content, and are showing as duplicates for SEO purposes, how do i redirect all ?p=1 queries in nginx to their query free counterpart.
Thanks
The value of the p argument is contained in the $arg_p variable. The if statement can be used to test the value of a variable. The actions available in an if block are limited (see this document for details), but a simple return statement is allowed.
So the simplest solution would be to obliterate the query string from any URI which contains p=1, like this:
server {
...
if ($arg_p = 1) {
return 301 $uri;
}
location / { ... }
}
Note that $uri is the normalised request URI, and is already missing the query string.
I have a web site which is translated to 18 languages. Some languages are the same (Brazilian is Portuguese). So I want to redirect from br to pt to avoid odd content, from /some_domain/br/... -> /some_domain/pt/...
I can write single redirect from one domain to another. Something like this:
location = /user/unique {
return 301 http://www.usgreencardoffice.com/blog/the-american-dream;
}
I want to achieve the following:
domain.com/br/something -> domain.com/pt/something
But for the languages redirection, I have no idea. How can I achieve this?
If the language code is at the beginning of the URI, a prefix location will be an efficient solution:
location ^~ /br/ {
rewrite ^/br(.*)$ /pt$1 permanent;
}
The ^~ modifier makes this prefix location take precedence over regex locations at the same level. If you change permanent to last, the rewrite becomes internal and thus invisible to the user.
See this and this for details.
Nginx, I am trying to permanently redirect the URLs with a device GET parameter (http://www.example.org/page?device=desktop) to the relative URL without this parameter (http://www.example.org/page).
I did this, but it doesn't work.
location {
rewrite ^(.*)\?device=desktop $1 permanent;
}
Each query parameter is exposed as a variable prefixed with $arg_ in the configuration file. For example, device would become $arg_device. Using this you can make the comparison check within your location block, for example:
location / {
if ($arg_device = desktop) {
return 301 $uri;
}
}
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.