Forum: NGINX Comprehensive Rewrite Rules / Regular Expressions Guide?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ole R. (Guest)
on 2009-04-01 14:04
Hey everyone,

I want to modify some rewrites I currently have in my nginx.conf, but I
could not find proper documentation of the available operators and
language constructs to do so. Googling I found tons of people trying to
convert rewrite rules from apache to nginx, but since I have never used
apache, this is not very helpful to me. Is there some document
explaining all supported operators for the regular expressions?

For example, I have something like $host ~* "www" in my config, and from
the context I can guess that ~* seems to check whether the left hand
side contains the right hand side. But I did not find a proper
specification in any regular expressions guides.

My current problem at the moment is that I want to say something like
"if the domain used is not already www.example.de, than change it that
way". I want that all example.com, example.org... are rewritten to read
www.example.de. So I need to know wether there is a possibility to
negate a condition, or something like "unless" or so. However, this is
only one problem I have right now, I would very much like to understand
the bigger picture and all the options I have in rewritting stuff. Is
there a document listing those options?
Jérôme Loyet (Guest)
on 2009-04-01 14:37
(Received via mailing list)
2009/4/1 Ole R. <removed_email_address@domain.invalid>:
> the context I can guess that ~* seems to check whether the left hand
> side contains the right hand side. But I did not find a proper
> specification in any regular expressions guides.

I think this should make the trick:

if ($host !~* "^www.exemple.de") {
  rewrite ^(.*)$ http://www.exemple.de$1 permanent;
}

>
> My current problem at the moment is that I want to say something like
> "if the domain used is not already www.example.de, than change it that
> way". I want that all example.com, example.org... are rewritten to read
> www.example.de. So I need to know wether there is a possibility to
> negate a condition, or something like "unless" or so. However, this is
> only one problem I have right now, I would very much like to understand
> the bigger picture and all the options I have in rewritting stuff. Is
> there a document listing those options?

http://wiki.nginx.org/NginxHttpRewriteModule
and
http://wiki.nginx.org/NginxHttpCoreModule#Variables
Igor S. (Guest)
on 2009-04-01 14:37
(Received via mailing list)
On Wed, Apr 01, 2009 at 12:27:49PM +0200, J?r?me Loyet wrote:

> > For example, I have something like $host ~* "www" in my config, and from
> > the context I can guess that ~* seems to check whether the left hand
> > side contains the right hand side. But I did not find a proper
> > specification in any regular expressions guides.
>
> I think this should make the trick:
>
> if ($host !~* "^www.exemple.de") {
>   rewrite ^(.*)$ http://www.exemple.de$1 permanent;
> }

No.

    server  {
         server_name  www.example.de;
         ...
    }

    server  {
         server_name  _; # any names

         rewrite   ^   http://www.example.de$request_uri? permanent;
    }
Ole R. (Guest)
on 2009-04-01 14:56
> No.
>
>     server  {
>          server_name  www.example.de;
>          ...
>     }
>
>     server  {
>          server_name  _; # any names
>
>          rewrite   ^   http://www.example.de$request_uri? permanent;
>     }

Thank you, this works just fine.

Thanks aswell for the wiki links, I hope they will help me with future
problems.
Michael S. (Guest)
on 2009-04-01 14:57
(Received via mailing list)
2009/4/1 Igor S. <removed_email_address@domain.invalid>:

>         rewrite   ^   http://www.example.de$request_uri? permanent;
>    }

What about this?

server {
        listen 80;
        server_name michaelshadle.com;
        index index.php index.html;
... etc ...
}

server {
        listen 80;
        server_name www.michaelshadle.com;
        rewrite ^(.*) http://michaelshadle.com$1 permanent;
}

Any better?

Any worse?
Igor S. (Guest)
on 2009-04-01 15:46
(Received via mailing list)
On Wed, Apr 01, 2009 at 03:50:53AM -0700, Michael S. wrote:

> > š š š š server_name š_; # any names
> ... etc ...
> Any worse?
rewrite ^ http://michaelshadle.com$request_uri? permanent;

slightly faster than

         rewrite ^(.*) http://michaelshadle.com$1 permanent;
This topic is locked and can not be replied to.