Rewrite directory lookup to another directory

AjaySawant nginx-forum at nginx.us
Sun Aug 24 05:05:50 UTC 2014


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 at symp html]# pwd
/usr/share/nginx/html
[root at symp html]# ls
404.html  50x.html  index.html  index.php  nginx-logo.png  poweredby.png 
testa  testb  testc
[root at 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 at 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 at symp html]# curl http://192.168.44.163/testa/
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.6.1</center>
</body>
</html>

[root at symp html]# curl http://192.168.44.163/testa
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.6.1</center>
</body>
</html>

Regards,
Ajay

Posted at Nginx Forum: http://forum.nginx.org/read.php?2,252780,252780#msg-252780



More information about the nginx mailing list