Rewrite directory lookup to another directory

Hello All,

I am trying to add a rewrite rule to my nginx configuration. But each
time
it is giving me 404/302 error. My scenarios is given below

  1. If someone request URL http://192.168.44.163/testa or
    http://192.168.44.163/testa/ then it should be redirected to
    http://192.168.44.163/testa/web/index.php or
    http://192.168.44.163/testa/web/index.html
  2. If someone request any file inside testa directory it should be
    redirected to testa/web directory and serve the file.

I tried with location /testa {} directive as well but in vain.

[root@symp html]# pwd
/usr/share/nginx/html
[root@symp html]# ls
404.html 50x.html index.html index.php nginx-logo.png poweredby.png
testa testb testc
[root@symp html]# tree
.
├── 404.html
├── 50x.html
├── index.html
├── index.php
├── nginx-logo.png
├── poweredby.png
├── testa
│ └── web
│ └── index.php
├── testb
│ └── web
│ └── index.php
└── testc
└── index.php

5 directories, 9 files
[root@symp html]# cat /etc/nginx/nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log;
#error_log /var/log/nginx/error.log notice;
#error_log /var/log/nginx/error.log info;
pid /run/nginx.pid;
events {
worker_connections 1024;
}

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local]
“$request”

'$status $body_bytes_sent “$http_referer” ’
‘“$http_user_agent” “$http_x_forwarded_for”’;

access_log  /var/log/nginx/access.log  main;
sendfile        on;
keepalive_timeout  65;
include /etc/nginx/conf.d/*.conf;
index   index.php index.html index.htm;
server {
    listen       80;
    server_name  192.168.44.163;
    root         /usr/share/nginx/html;
    access_log  /var/log/nginx/192.168.44.163.access.log  main;
    error_log /var/log/nginx/192.168.44.163.error.log;
    location / {
    }
    rewrite ^/testa/(.*)$   /testa/web/$1 permanent;
    rewrite ^/testb/(.*)$   /testb/web/$1 permanent;
    error_page  404              /404.html;
    location = /40x.html {
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
    }
    # pass the PHP scripts to FastCGI server listening on

127.0.0.1:9000
location ~ .php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}

[root@symp html]# curl http://192.168.44.163/testa/

301 Moved Permanently

301 Moved Permanently


nginx/1.6.1

[root@symp html]# curl http://192.168.44.163/testa

301 Moved Permanently

301 Moved Permanently


nginx/1.6.1

Regards,
Ajay

Posted at Nginx Forum: