Nginx 301 redirect with wildcard - nginx

Need to create a nginx 301 redirect.
From: /blog/some-article
To: /blog/article/some-article
Current nginx configuration:
location /blog/(.*)$ {
rewrite ^/blog/(.*)$ ^/blog/article$1 redirect;
}
Current configuration result:
This URL with multiple 301 redirects
/blog/article/article/article/article/article/article/article/article/article/article/article/article/article/article/article/article/article/article/some-article
How to configure this redirect properly?
Any help will be appreciated, thanks.

You can use a negative lookahead assertion in the regular expression to avoid the redirection loop.
This needs to be done at the location level.
You can avoid using two regular expressions by using a return statement instead or the rewrite. rewrite...redirect is implemented as return 302, whereas a rewrite...permanent would be implemented as return 301.
For example:
location ~ ^(/blog/)((?!article/).+)$ {
return 301 $1article/$2;
}

Related

Nginx redirect being ignored

I'm trying to create a redirect for all URLs on a domain that contains /comfy/ to another domain, but keeping the rest of the request.
For example
https://www.example.org/system/comfy/cms/files/files/000/002/012/profile/AdobeStock_194204879.jpeg
to
https://www2.example.org/system/comfy/cms/files/files/000/002/012/profile/AdobeStock_194204879.jpeg
In nginx in the server block for example.org I have
location ~ ^/comfy/(.*) {
return 301 $scheme://www2.example.org/$request_uri;
}
but i'm still getting a 404, with the original domain, ie it's still accessing
https://www.example.org/system/comfy/cms/files/files/000/002/012/profile/AdobeStock_194204879.jpeg
I have also tried
rewrite ^(/comfy/)(.*)$ http://www2.example.org/$2 permanent;
and
rewrite ^/comfy/(.*) http://www2.example.org/$1 break;
but these are failing too. What am I missing?
Thanks!
Ok, turns out I needed to do
location ~ ^/system/comfy/cms/(.*) {
return 301 $scheme://www2.example.org/$request_uri;
}

Nginx Redirects

I'm struggling with a nginx redirect that I've set up.
Basically, I want to redirect /case-study/ to /case-studies/ but don't want to redirect any url past /case-study/*.
I have this setup already:
location = /case-study/ {
rewrite ^(.*)$ /case-studies/ permanent;
}
Which works for /case-study/ but also redirects any url after /case-study/test-url-redirect
Cany anyone help me out on this one.
The simplest way is to make it a regex location like so:
location ~ /case-study(/?)$ {
return 301 /case-studies/;
}
Should match /case-study and /case-study/ and redirect to /case-studies/ but not redirect anything that is under case-study

NGINX Subdirectory Redirect is not working

TLDR: subdirectory/cool redirects to newdirectory/cool. subdirectory?cool incorrectly redirects to newdirectory without the parameter.
The Prob:
I'm attempting to redirect a subdirectory to a new directory while preserving the original URL parameters via NGINX location using the regex ~* ^(?:((\/m\b)(?!-)(\/)?))(?<requestedPath>.*)?$
The paths such as /subdirectory/cool successfully redirect to /newdir/cool. However, paths like /subdirectory?cool and /subdirectory/?cool are redirecting to /newdir without preserving the URL parameters.
The redirect should work as follows:
/subdirectory -> /newdir
(Many of the suggestions I've seen posted here for subdirectrory redirects assume a trailing slas or do not take into account that their regex for /m also impacts /media)
/subdirectory/ -> /newdir/
/subdirectory/test -> /newdir/test
/subdirectory/?apple -> /newdir/?apple
/subdirectory?apple -> /newdir?apple
The test is successful here: Successful Test
The code that is failing:
location ~* ^(?:((\/subdirectory\b)(?!-)(\/)?))(?<requestedPath>.*)?$ {
return 301 /newdir/$requestedPath;
}
Additionally, I have also tried using rewrite instead of location to no avail.
Apparently, with location, one has to us $is_args.
This did the trick: return 301 /newdir/$requestedPath$is_args$args;
End result:
location ~* ^(?:((\/subdirectory\b)(?!-)(\/)?))(?<requestedPath>.*)?$ {
return 301 /newdir/$requestedPath$is_args$args;
}

Chage the part of the URL using nginx

I m using nginx webserver.
I want to change the url before it hits the server from
https://www.example.com/abc/contact-us
to
https://www.example.com/#/contact-us
Thanks in advance.
For a single URI redirection, an exact match location and return statement may be most efficient:
location = /abc/contact-us {
return 301 /#/contact-us;
}
To redirect all URIs beginning with /abc use a rewrite directive:
location ^~ /abc/ {
rewrite ^/abc(.*)$ /#$1 permanent;
}
The location block is largely redundant, but means nginx only looks at the regular expression when it needs to. See this document for more.

Nginx - Redirect Domain Trailing Dot

How can I redirect "http://domain.com." to "http://domain.com" with Nginx?
What's the recommended way of doing this? Regex or is there any other options?
The following snippet does this in a general way, without having to hard code any hostnames (useful if your server config handles requests for multiple domains). Add this inside any server definition that you need to.
if ($http_host ~ "\.$" ){
rewrite ^(.*) $scheme://$host$1 permanent;
}
This takes advantage of the fact (pointed out by Igor Sysoev) that $host has the trailing dot removed, while $http_host doesn't; so we can match the dot in $http_host and automatically use $host for the redirect.
You will need to use Regex.
server {
listen 80;
server_name domain.com.WHATEVER, domain.com.WHATEVER-2, domain.com.WHATEVER-3;
rewrite ^ $scheme://domain.com$request_uri? permanent;
}
From: http://wiki.nginx.org/HttpRewriteModule
redirect - returns temporary redirect with code 302; it is used if the substituting line begins with http://
permanent - returns permanent redirect with code 301

Resources