Rewrite rules issue

Hello,

I have an issue with a rewrite rule for redirect to a subdomain. Here it
is:

if ($http_host = “subdomain.domain.fr”) {
rewrite ^(?!/\b(subpath|stats|error)\b)/(.*)$ /subpath/$2
last;
}

if in my browser i write:
host.domain.fr/admin (without last /), then I’m redirected to
host.domain.fr/host.domain/admin/ (with a 404 error of course)

But if i write:
host.domain/admin/ (with last /), then all works fine.

I don’t understand what is the configuration issue. I want to have the
same
behaviour with or without last /
Any help would be helpfull.

Best regards,
Nico

Posted at Nginx Forum:

On Sat, Apr 18, 2015 at 04:08:54PM -0400, nicocolt wrote:

Hi there,

if ($http_host = “subdomain.domain.fr”) {
rewrite ^(?!/\b(subpath|stats|error)\b)/(.*)$ /subpath/$2 last;
}

if in my browser i write:
host.domain.fr/admin (without last /), then I’m redirected to
host.domain.fr/host.domain/admin/ (with a 404 error of course)

Are you reporting that when you have the three lines above in your
config,
you get this behaviour; and when you remove those three lines from your
config, you do not get this behaviour?

Because that seems strange.

Thanks,

f

Francis D. [email protected]

Hello

Thanks for your reply.

If I remove the three lines, i get a 404 error. As a reminder, and maybe
a
little bit more clear :slight_smile: here is the configuration.

Here is the directory path:

/var/www/domain.fr/web/subdomain/directory

my nginx conf is:

root /var/www/domain.fr/web

if ($http_host = “subdomain.domain.fr”) {
rewrite ^(?!/\b(subdomain|stats|error)\b)/(.*)$
/subdomain/$2
last;
}

The if statement is before all of location directives.

After purging my cache browser, this is what I get:

when i reach http://subdomain.domain.fr/directory i’m redirected to
http://subdomain.domain.fr/subdomain/directory (this is not what i want,
i
want http://subdomain.domain.fr/directory)

Thanks fo your help,
Best regards,
Nico

Posted at Nginx Forum:

On Sun, Apr 19, 2015 at 04:06:55AM -0400, nicocolt wrote:

Hi there,

last;
}

The if statement is before all of location directives.

After purging my cache browser, this is what I get:

when i reach http://subdomain.domain.fr/directory i’m redirected to
http://subdomain.domain.fr/subdomain/directory (this is not what i want, i
want http://subdomain.domain.fr/directory)

Ok, this makes a bit more sense now: your request actually might use
the configuration shown.

Are you sure that you have copy-pasted exactly the response? I would
have
expected a redirect to http://subdomain.domain.fr/subdomain/directory/
– but the actual redirect comes from the configuration that you have
not yet shown.

Your rewrite says that you do want the request to be handled like
http://subdomain.domain.fr/subdomain/directory, so I’m still not sure
what the actual full end intention is.

It does look to me like

server_name subdomain.domain.fr;
root /var/www/domain.fr/web/subdomain;

is probably what you really want, but I guess there is some reason why
you don’t just use that?

Good luck with it,

f

Francis D. [email protected]

Hello,

you’re right, my rewrite says that my request is handle like
http://subdomain.domain.fr/subdomain/directory/. But it is not that i
want.

I want that my request is handled like this:

http://subdomain.domain.fr/directory/ and i don’t want see the
intermediate
path. I don’t understand how to proceed in order to see only
http://subdomain.domain.fr/directory/ instead of
http://subdomain.domain.fr/subdomain/directory/

Thaks for your help !
Best regards,
Nico

Posted at Nginx Forum:

On Sun, Apr 19, 2015 at 09:53:43AM -0400, nicocolt wrote:

Hi there,

I don’t understand how to proceed in order to see only
http://subdomain.domain.fr/directory/ instead of
http://subdomain.domain.fr/subdomain/directory/

I’ll still suggest

root /var/www/domain.fr/web/subdomain;

in a separate server{} block, and then have /stats/ and /error/
locations
which do whatever else you want them to do.

Good luck with it,

f

Francis D. [email protected]

Hi Francis,

I have set a new server block, but i’m facing with the initial problem.

So let me re-explain it.

Now i have:

server name stuff.domain.fr
root /var/www/domain.fr/web/subdomain;

In the subdomain directory i have foo firectory
/var/www/domain.fr/web/subdomain/foo/

so now in my browser when i reached stuff.domain.fr i see the page
index.html in subdomain folder. But if i reached

stuff.domain.fr/foo, i’m redirected to stuff.domain.fr/subdomain/foo/
404
stuff.domain.fr/foo/, i see the page index.html in foo folder

Well i’m lost, two days trying to make this work. Seems that the
trailling
slah has an impact on how the request is handle by nginx.

Thanks for your help
Best regards,
Nico

Posted at Nginx Forum:

On Mon, Apr 20, 2015 at 04:12:11AM -0400, nicocolt wrote:

Hi there,

I have set a new server block, but i’m facing with the initial problem.

So let me re-explain it.

Now i have:

server name stuff.domain.fr
root /var/www/domain.fr/web/subdomain;

On your test system which shows this behaviour, what is the complete
copy-paste version of the server{} configuration that you are using?

If I use

==
server {
server_name stuff.domain.fr;
root /var/www/domain.fr/web/subdomain;
}

then “curl -i -H Host:stuff.domain.fr http://127.0.0.1/foo” does not
show me the problem that you report.

f

Francis D. [email protected]

Hello Francis,

server {
listen *:80;

    server_name domain.fr www.domain.fr subdomain.domain.fr;

    root   /var/www/domain.fr/web;

    if ($http_host = "subdomain.domain.fr") {
        rewrite ^(?!/\b(bar|stats|error)\b)/(.*)$ /bar/$2 last;
    }


    index index.html index.htm index.php index.cgi index.pl

index.xhtml;

    error_page 400 /error/400.html;
    error_page 401 /error/401.html;
    error_page 403 /error/403.html;
    error_page 404 /error/404.html;
    error_page 405 /error/405.html;
    error_page 500 /error/500.html;
    error_page 502 /error/502.html;
    error_page 503 /error/503.html;
    recursive_error_pages on;
    location = /error/400.html {
        internal;
    }
    location = /error/401.html {
        internal;
    }
    location = /error/403.html {
        internal;
    }
    location = /error/404.html {
        internal;
    }
    location = /error/405.html {
        internal;
    }
    location = /error/500.html {
        internal;
    }
    location = /error/502.html {
        internal;
    }
    location = /error/503.html {
        internal;
    }

    error_log /var/log/ispconfig/httpd/domain.fr/error.log;
    access_log /var/log/ispconfig/httpd/domain.fr/access.log 

combined;

    location ~ /\. {
        deny all;
        access_log off;
        log_not_found off;
    }

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    location /stats/ {

        index index.html index.php;
        auth_basic "Members Only";
        auth_basic_user_file

/var/www/clients/client0/web5/web/stats/.htpasswd_stats;
}

    location ^~ /awstats-icon {
        alias /usr/share/awstats/icon;
    }

    location ~ \.php$ {
        try_files /95c54bbc57ae02e6bba619001d015e75.htm @php;
    }        location @php {
        try_files $uri =404;
        include /etc/nginx/fastcgi_params;
        fastcgi_pass unix:/var/lib/php5-fpm/web5.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME

$document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
}
}

So if host is subdomain.domain.fr, then the url is rewrite to
subdomain.domain.fr/bar/

Then now if in bar i have foo

if i try to reach http://subdomain.domain.fr/foo/ then ok
if i try to reach http://subdomain.domain.fr/foo then i’m redirect to
http://subdomain.domain.fr/bar/foo/ NOT ok

Thanks for your help !
Best regards,
Nico

Posted at Nginx Forum:

On Tue, Apr 21, 2015 at 09:26:59AM -0400, nicocolt wrote:

Hi there,

server {
listen *:80;

    server_name domain.fr www.domain.fr subdomain.domain.fr;

This doesn’t match what I thought you had done.

So if host is subdomain.domain.fr, then the url is rewrite to
subdomain.domain.fr/bar/

Then now if in bar i have foo

if i try to reach http://subdomain.domain.fr/foo/ then ok
if i try to reach http://subdomain.domain.fr/foo then i’m redirect to
http://subdomain.domain.fr/bar/foo/ NOT ok

I suspect I’m not going to be able to give you the answer you want,
so I’ll leave it for someone else.

Good luck with it,

f

Francis D. [email protected]

Hi Francis

Ok. Many thanks for your interest of my problem.

Best regards
Nico

Posted at Nginx Forum: