redirect to http depending on condition - linux

I use nginx
Assume that url hello.test.example.com, hello example of dynamically subdomain, how can i redirect to hello.test2.example.com when hello file existing in specific directory path
server_name ~^(?<subdomain>[^.]+)\.test\.example\.com$;
set $m_file /home/files/$subdomain;
if (-f $m_file) {
rewrite ^/(.*)$ http://$subdomain.test2.example.com/;
}
this condition working when the url (hello.test.example.com) but when the url (hello.test.example.com\en) working but not every time ! why ?

Related

Nginx parse and redirect to other domain

I wanna make some redirect but for this i need to parse domain to send other domain.
My old domain url like this
http://olddomain.com/bg/some-name-part-421.html
http://olddomain.com/bg/some-name-1231.html
http://olddomain.com/bg/some-name-product-name-221.html
I want to redirect this to like this
https://www.newdomain.com/magazin/some-name-part.html
https://www.newdomain.com/magazin/some-name.html
https://www.newdomain.com/magazin/some-name-product-name.html
I try to redirect them like this on server block
rewrite ^(/bg/)([a-z-]+-[0-9]+)\.html$ http://www.newdomain.com/magazin/$2 permanent;
Not working well making redirect like this
http://www.olddomain.com/bg/chervena-borovinka-bioherba-3694.html
https://www.newdomain.com/magazin/chervena-borovinka-bioherba-3694
I want to delete also as last part of number and - but i dont know why not working well
Thats my .htaccess:
RewriteEngine On
RewriteRule "^bg\/([^0-9]+(?<!-))-([0-9]+)(\.html)" "http://newdomain.com/magazin/$1$3" [R]
Dont forget to set the RewriteBase.
Heres the code: https://regex101.com/r/UdMqaQ/3/
Nginx
rewrite "^/bg\/([^0-9]+(?<!-))-([0-9]+)(\.html)" "newdomain.com/magazin/$1";

nginx rewrite not working... grab file with wildcard

The example below is what I have tried with no luck. I would like to redirect all files in the folder /files/dump/ that start with 15EIP to /file-is-gone
rewrite ^/files/dump/15EIP(*.*)?$ /file-is-gone permanent;

Nginx duplicating query string on redirect

I am trying to redirect all traffic from urls that contain 'www.' to the similar url that does not contain it, but when I do so Nginx appends a duplicate query string at the end of the url.
For example, the user enters:
www.website.com/test/?_id=12345
And they get redirected to:
website.com/test/?_id=12345?_id=12345
Here is my config:
server {
server_name www.website.com;
return 301 $scheme://website$request_uri permanent;
}
server {
server_name website.com
# actual server stuff
}
I have tried appending a '?' to the end of the redirect after the 'request_uri' portion because form what I have read that should work, but it didn't.
Your config is invalid. You seem to have mixed up the syntax of return and rewrite. See this document for details.
The $request_uri variable already includes the query string, so this should work with return:
return 301 $scheme://example.com$request_uri;
The rewrite directive will append the query string unless a ? is appended. It is possible to use a rewrite directive to accomplish the same function, but in this case it would be overkill.
As your config is invalid, nginx is still running with an earlier configuration, possibly an earlier experiment that placed $request_uri in a rewrite directive, hence the double query string.

301 is not working on nginx server

A magento website transferred to Nginx and my 301 redirects are not working here.
Previous URL:
www.domain.com/store/food/two-year-supply-of-glide-r-chow-glide-a-mins.html
New URL:
www.domain.com//two-year-supply-of-glide-r-chow-glide-a-mins.html
Initially my .htaccess was
Redirect 301 /store/food/two-year-supply-of-glide-r-chow-glide-a-mins.html /two-year-supply-of-glide-r-chow-glide-a-mins.html
and now I have convert it to Nginx server format i.e.
location /store/food/two-year-supply-of-glide-r-chow-glide-a-mins.html {
rewrite ^(.*)$ /two-year-supply-of-glide-r-chow-glide-a-mins.html redirect;
}
Which is not working.
You conversion looks fine to me. sometime nginx server ignore .htacess file try with a new file named as nginx.conf and put your all htacess conversion in this.
Edit
# nginx configuration
location /store { rewrite ^/store/food/(.*)$ /$1 redirect; }
I think this might help you.

How to redirect a site to a new site

I have a site which has pages like this:
blabla.com/page/whatever
blabla.com/category/whatever
blabla.com/about
...
How can I redirect each of these to a new domain, like:
blabla.net/page/whatever
blabla.net/category/whatever
blabla.net/about
...
?
Using .htaccess
Use the Redirect directive:
Redirect / http://blabla.net/
This directive automatically preserves anything specified after the /.
It might take a bit of fiddling, but the basic idea should work here:
RewriteEngine on
RewriteRule ^(.+)$ http://blabla.net/$1 [R,NC]
You need to have mod_rewrite installed in Apache.
This says "match all URLs on this site, and redirect them to http://blabla.net/the same URL. The [R] means to actually send a redirect request to the client (so the client will make the request to the new server), rather than just serving up the page but keeping the browser URL the same. You can take the R out if you just want to serve the page but keep the old URL.
Or if you use nginx (like we at http://applehub.us, http://crazyfootball.net etc)
location ~ ^/.*_sitemap([\d]+)?.(xml|xml.gz)$ {
rewrite /(.*) /$1 break;
proxy_pass http://yourupstrem;
}

Resources