Nginx redirect being ignored - nginx

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;
}

Related

Nginx 301 redirect with wildcard

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;
}

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 URL rewrite

I have a website that has laravel setup to run under http://www.example.com/lara/
So, most Laravel pages have URLs of type http://www.example.com/lara/page/23 OR http://www.example.com/lara/category/23 etc.
Nginx is the underlying server and it has the following configuration to handle these requests:
location /lara/ {
try_files $uri $uri/ /lara/index.php?$query_string;
}
Everything works ok.
Now, I need to setup a special page with the URL http://www.example.com/mystuff/ which actually is handled by
http://www.example.com/lara/category/29
To get this working I added the following rewrite right below location /lara/, that is:
rewrite ^/mystuff/(.*)$ /lara/category/29/$1 last;
Unfortunately, I get a page not found error. Any insights?
Further investigation & research:
1)
location /mystuff/ {
return 301 /lara/index.php/category/29;
}
worked although that's not (browser address bar changes to) what I actually want.
2) Looks like Laravel is not seeing the updated REQUEST_URI.
Try this
server {
...
rewrite ^/lara/(.*)\..*$ $1/mystuff last;
return 403;
...
}

nginx 301 rewrite root domain works but files are not being redirected

I'm trying to redirect a domain to another url in nginx and it works partially.
It looks like this:
location / {
rewrite ^/(.*) http://www.some_domain.tld/some_dir/some_file.php permanent;
}
It redirects fine the root domain, however if I try to load http://www.my_old_domain.tld/some_file.php it won't do the same. It'll load the page.
Thoughts ?
Thanks.
Try using this
location /
return 301 http://newdomain.com/a/b/c.php;
}

How to redirect any request to a specific page if the visitor was using a certain name?

How can I get visitors redirected to a specific html page if the name used to resolve the server address was a specific one? I tried
if ($http_host ~ /forbiddenname/)
{
rewrite ^(.*)$ /updateyourlinks.html break;
}
inside the Server section, but doesn't work...
After some research I found that I should use instead of an if a virtual host... what I've added to nginx configuration is another server
server {
listen 80;
server_name *.badname.com;
rewrite ^ http://goodname.com/updateyourlinks.html;
}
and this apparently works

Resources