I want to redirect all /filename.xml files to /filename.
I can redirect an individual file like this
location = /mascus-export.xml {
rewrite .* /mascus-export redirect;
}
but rather than listing each .xml file like individually, I want redirect all. Something like this:
location = /*.xml {
rewrite .* /* redirect;
}
I've tried this type of thing in every combination
location = /(*)$.xml {
rewrite .* /$1 redirect;
}
but nothing seems to work.
You are trying to write a regular expression location block, which begins with ~ and not =. See this document for details.
For example:
location ~ \.xml$ { ... }
The rewrite needs to capture part of the URI using parentheses. For example:
rewrite ^(.*)\.xml$ $1 redirect;
The rewrite will work perfectly well either enclosed within the above location block, or just naked within the enclosing server block.
If you are going to use a regular expression location block, you do not need to use a rewrite statement too. Use a return statement instead. For example:
location ~ ^(.*)\.xml$ {
return 302 $1;
}
Related
I'm trying to create an nginx rewrite that will rewrite /pagename to /pagename/
I've tried using:
location ~ "^/test$" {
rewrite /test /test/ break;
}
but that doesn't seem to work...
any help would be appreciated.
The first parameter of a rewrite statement is a regular expression, and the flag you should use is probably permanent (see this document for details), for example:
location ~ "^/test$" {
rewrite ^(/test)$ $1/ permanent;
}
But you do not need to match the regular expression twice, once in the location and again in the rewrite, so a return would be more efficient, for example:
location ~ "^(/test)$" {
return 301 $1/$is_args$args;
}
Also, the location matches a single URI, to the = operator would be better than a regular expression (see this document for details). So the preferred solution is:
location = /test {
return 301 $uri/$is_args$args;
}
I ended up using return:
return 301 /test/;
I would like to have a lighter code for one of my NGINX configurations. I need to write the following four lines in only one (if possible!):
location /en/contact {
rewrite ^/.* https://$server_name/contact permanent;
}
location /en/quality {
rewrite ^/.* https://$server_name/quality permanent;
location /de/contact {
rewrite ^/.* https://$server_name/contact permanent;
}
location /de/quality {
rewrite ^/.* https://$server_name/quality permanent;
}
Thanks!
Using minimal typing, you could use a rewrite statement outside of any location block:
rewrite ^/(en|de)/(contact|quality) https://$server_name/$2 permanent;
If the scheme and server name does not change, the second parameter can be replaced with just /$2. See this document for more.
Alternatively, you could use a regular expression location block:
location ~ ^/(en|de)/(contact|quality) {
return 301 https://$server_name/$2;
}
Same as above, regarding the scheme and server name. Note that the evaluation order for regular expression location blocks is significant - the first rule that matches wins. See this document for more.
I try to redirect some URLS with Nginx but having problems to combine filtering and stripping.
Redirect:
location ~* "^/7_skin/skin_archi/skin_control/text_box$" {
if ($request_uri ~* "([^/]*$)" ) {
set $last_path_component $1;
}
return 301 https://www.domain.com/wiki/display/$last_path_component;
}
this works fine and filters every path but last. Now I would like to strip characters (- and _) from $last_path_component but that doesn't seem to work.
I tried something like: rewrite ^(.*)[-_](.*)$ $1$2 permanent;
which worked, but not in conjunction with first filter as the whole path is given out.
I have a location and rewrite directive pair that works, but there is a redundancy that it seems like I should be able to optimize out. It takes an external URL like /css/20141201-styles.css and delivers /css/styles.css.
location ~ '^/(css|js)/[0-9]{8}-' {
rewrite '^/(css|js)/[0-9]{8}-(.*)$' /$1/$2;
}
It seems like I'm doing work twice, once in matching, and then again when rewriting. Is there a way to capture the matches in the location directive, and then use them in the rewrite?
location ~ '^/(css|js)/[0-9]{8}-(.*)$' {
rewrite [something here?] /$1/$2;
}
In Apache, it looks like this:
RewriteRule ^/(css|js)/[0-9]{8}-(.*)$ /$1/$2 [NC,L]
SOLUTION
Thanks to Terra for pointing out the alias directive! This location was already nested in another location which applies the root directive, so that takes care of the server path.
location ~ ^/(css|images|js)/ {
location ~ '^/(css|js)/[0-9]{8}-(.*)$' {
alias /$1/$2;
}
root /server/path/to/web/root;
}
UPDATE
This also works, and has fewer lines. The break flag on the rewrite directive prevents looping of the server level, making it just as performant as the alias solution.
location ~ ^/(css|images|js)/ {
rewrite '^/(css|js)/[0-9]{8}-(.*)$' /$1/$2 break;
root /server/path/to/web/root;
}
Solution 1: Do not create separate location, just move rewrite to other one. location / for example.
Solution 2:
location ~ ^/(css|js)/[0-9]{8}/ {
alias /server/path/to/web/root/$1/;
}
I'm completely new to nginx and I was wondering if someone could give me a hand.
What I'm trying to to is turn this:
domain.com/ngu/short.php?t=123
into
domain.com/t/123
If someone could explain to me how it's done, I would be very grateful. I have looked around the site for a similar situation, but I haven't found anything exactly the same so I'm having some difficulty. Thank you for your time :).
the following location block does the rewrite:
location ~ /ngu/short.php {
if ($args_t) {
rewrite ^ http://$host/t/$v? last;
}
}
but that requires you enumerate all possible arguments, if you have lots of potential arguments it's probably easier to do
location ~ /ngu/short.php {
if ($args ~ "([a-z]+)=(\d+)") {
set $p $1;
set $v $2;
rewrite ^ http://$host/$p/$v? last;
}
}
explanation:
we do a location that matches on the /ngu/short.php path
the first code block above checks if we have a t=value argument and if so does the rewrite
the second code block above checks for a more general argument=something. We then need to save the backreferences in variables because a rewrite rule resets the backreferences which prevents using them directly
in either case if the if fails to match a 404 will be returned
$host and $args en $args_ are standard nginx variables see http://wiki.nginx.org/HttpCoreModule#Variables
we add a ? ad the end of the rewrite to keep nginx from readding the url arguments
In case this helps anyone else, this is what I (actually a friend) ended up doing:
location / {
root /home/jim/www;
index index.html index.htm index.php;
if (!-f $request_filename) {
rewrite ^/t/(.+)$ /ngu/short.php?t=$1 last;
rewrite ^/u/(.+)$ /ngu/short.php?u=$1 last;
rewrite ^/s/ /ngu/short.php last;
break;
}
}
I don't understand it, but it works.