301 is not working on nginx server - .htaccess

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.

Related

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;

Redirecting the content of a directory, except for the directory itself and some of its subdirectories and files on Nginx

#Starkeen solved this for an Apache configuration at Redirecting the content of a directory, but not the directory itself as well as some of its subdirectories and files, but we'll soon be moving to Nginx.
His very elegant solution for Apache:
RedirectMatch ^/category/((?!index|images|menu)[^/]+)/?$ /$1
Here, everything in the /category/ directory -- except for /category/index.php, the content of /category/images/ and the content of /category/menu/ -- is redirected to the root folder.
We tried the translation from htaccess to Nginx offered at http://winginx.com/en/htaccess,
location ~ ^/category/((?!index|images|menu)[^/]+)/?$ {
rewrite ^(.*)$ /$1 redirect;
}
but that didn't work.
For some reason, when we finally give up searching for a solution and end up asking for help, we often find one soon after. Here it is -- it works perfectly:
rewrite ^/category/((?!index|images|menu)[^/]+)/(.*)$ /$1 permanent;

Nginx rewrite doesn't work when I try to match a file

I need to convert in Nginx these .htaccess rules, but when I try to target the filename and its extensions, Nginx doesn't match it. I really don't understand why of this behavior.
RewriteRule ^(.*?)(test_mod_rewrite) /$1media/plg_jchoptimize/assets/modrewrite.php?q=$2
RewriteRule ^(.*?)(gz|nz)/([^/]+)/([^/]+)/([^/]+)\.(js|css)$ /$1media/plg_jchoptimize/assets/jscss.php?f=$5&type=$6&gz=$2&d=$3&i=$4
I converted these rules in Nginx:
location /media/plg_jchoptimize/assets/ {
rewrite ^/(test_mod_rewrite) /media/plg_jchoptimize/assets/modrewrite.php?q=$1;
rewrite ^/(gz|nz)/([^/]+)/([^/]+)/([^/]+)\.(js|css)$ /$1media/plg_jchoptimize/assets/jscss.php?f=$4&type=$5&gz=$1&d=$2&i=$3;
}
The example URL is:
/media/plg_jchoptimize/assets/gz/30/0/d8604b25d503f1dcbb035ec731857648.css
What I'm wrong?

htaccess - 301 redirect with GET params

I have a problem. The Сustomer, has changed permalinks on his site and ask me to set 301 redirect to old permalinks.
Old version: http://my-site.by/catalog/?branch=3855
New version: http://my-site.by/catalog/3855/
I tried in different ways, but it does not work...
Can anyone help me?
UPD
Oh, Sorry. Quite forgot to say.
I'm tired to pick up and do this on PHP side :)
MY SOLUTION:
if (strpos($_SERVER['REQUEST_URI'], '/catalog/?branch=') !== false)
{
header(«HTTP/1.1 301 Moved Permanently»);
header(«Location: www.my-site.by/catalog/».str_replace("/catalog/?branch=","",$_SERVER['REQUEST_URI'])."/");
exit();
}
someone else may be needed.
Add this to you htaccess :
# 301 Redirect - url to url
RewriteRule http://my-site.by/catalog/?branch=3855 http://my-site.by/catalog/3855/ [R=301,L,NC]
And if you want to do this for all this kind of url :
# 301 Redirect - Dynamic Rewriting
RewriteRule ^catalog/?branch=([0-9]+)$ catalog/$1/

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