RedirectMatch 301 equivalent in Nginx - .htaccess

I'm a Apache user trying to use Nginx and I really can't understand how to convert RedirectMatch 301. Can someone give a better explanation of how it works?
.htaccess version:
RedirectMatch 301 /(tf2|cs|dota2|portal|hl|l4d|steam)/([a-zA-Z0-9-_]+)/([a-zA-Z0-9-_/]+) /$1/$3
My try on /etc/nginx/sites-available/default file:
server {
rewrite "^/(tf2|cs|dota2|portal|hl|l4d|steam)/([a-zA-Z0-9-_]+)/([a-zA-Z0-9-_/]+)" /$1/$3 last;
}
It's intend to redirect for eg. mysite.com/tf2/this-will-be-removed/this-will-not/ to mysite.com/tf2/this-will-not/
Thanks!

It should be :
server {
server_name mysite.com;
location / {
rewrite "^/(tf2|cs|dota2|portal|hl|l4d|steam)/([-\w]+)/([-\w/]+)$" /$1/$3 permanent;
}
}

Related

Nginx rewrite subdirectory

I'm trying to do a rewrite for nginx. It's working but for sub directories it is taking the root's index.php instead of the index.php in the individual subdir. This is how my directory structure looks like:
/index.php
/p/index.php
/c/index.php
and this is my rewrite:
if (!-e $request_filename){
rewrite ^/([^./]+)/?$ /index.php?act=$1 break; #need break?
rewrite ^/([^./]+)/(.+)/?$ /index.php?act=$1&upm=$2 break;
rewrite ^/([^./]+)/([^./]+)/?$ /$1/index.php?act=$2;
}
I tried adding an entry for each subdirectory but it doesn't work either:
location /p/ {
root /home/user/public_html/p/;
rewrite ^/([^./]+)/(.+)/?$ /index.php?act=$1&upm=$2 break;
}
Any ideas?
The rewrite ^/([^./]+)/([^./]+)/?$ /$1/index.php?act=$2; will never be executed as the previous rewrite will always match. You might try swapping rewrites #2 and #3.
The root /home/user/public_html/p/; is probably wrong as /p/ will appear twice as it is in the $document_root and the $uri.
The rewrite in your location /p/ should rewrite to /p/index.php and not /index.php?

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.

Rewrite Results in NGINX

The RewriteRule written in a CentOS environment doesn't work with NGINX environment even after conversion.
Can anyone guide me, what syntax needs to be followed in an NGINX environment for Drupal rewriting rules?
For URL rewrites in Drupal, please put the following in your nginx vhost configuration within the location / section of the server section:
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php?q=$1 last;
break;
}
As described here: https://www.drupal.org/node/110224

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?

How to redirect urls on a NGINX environment?

I am trying to redirect from:
http://domain.com/public/photos/large/test.jpg
to:
http://domain.com/images/public/photos/large/test.jpg
on a NGINX environment.
My vhost file is configured like the next lines.
My problem is that a syntax like this is not working:
server {
........
rewrite ^(public/photos/(.*)) http://domain.com/images/$1 last;
location / {
location ~.*\.(3gp|jpg|jpeg|gif)$ {
expires 7d;
try_files $uri #backend;
}
}
..........
}
The rewriterule syntax was tested on a .htaccess environment and it worked for more than 2 years now. On nginx it doesn't anymore.
Any ideas?
10x
Try this:
rewrite ^/public/photos/(.*)$ http://domain.com/images/public/photos/$1 last;

Resources