Regex rewrite problem

Hello,

I am new to this forum. Glad it exists. Thanks for that. I have a
production website that uses nginx 0.8.53 reverse proxy in front of
apache. I actually had little problem setting that up, but this simple
regex is giving me a headache.

For some reason, I am unable to take a URI like
example.com/manufacturers/metlox/0?page=13 and change it to
example.com/manufacturers/m It seems like the curly braces aren’t
working.

I’ve tried the following without results:

location ~* /manufacturers/ {
rewrite "^/manufacturers/(.{1})/(.)$"
/manufacturer/directory/$1 redirect;
}
location ~
/manufacturers/ {
rewrite “^/manufacturers/([a-z] {1})/(.*)$”
/manufacturer/directory/$1 redirect;
}

location ~* /manufacturers/ {
rewrite “^/manufacturers/([a-z]?)/(.*)$”
/manufacturer/directory/$1 redirect;
}

I am fairly new to regex, so maybe this is something simple. What I want
to get is the first letter of first captured word. I am able to pull
the entire word with (.*) for $1, but not the first letter only.

Any help would be greatly appreciated!

Thanks in advance.

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

working.
}
Any help would be greatly appreciated!

Thanks in advance.

Hello.

Try the following:

location /manufacturers/ {
location ~ ^/manufacturers/(.) {
return 301 /manufacturers/$1;
}
}

Brilliant! It works perfectly. Do you have any idea why the curly braces
didn’t work and also where I can learn when to use a location in a
location? Thank you so much!

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

On 19 Jan 2012 00h31 WET, [email protected] wrote:

Brilliant! It works perfectly. Do you have any idea why the curly
braces didn’t work and also where I can learn when to use a location
in a location? Thank you so much!

Regexes with curly braces have to be quoted.

From the wiki: http://wiki.nginx.org/HttpRewriteModule#rewrite

 Note: for curly braces ('{' and '}'), as they are used both in
 regexes and for block control, to avoid conflicts, regexes with
 curly braces are to be enclosed with double quotes (or single
 quotes). For example, to rewrite URLs like

 rewrite  "/photos/([0-9] {2})([0-9] {2})([0-9] {2})" 

/path/to/photos/$1/$1$2/$1$2$3.png;

— appa

On Thu, Jan 19, 2012 at 10:46 AM, mpratt [email protected] wrote:

everything according to that section…

Of course it won’t. The regex above will only match url like this:

/manufacturers/a/something

and not:

/manufacturers/asdfgh/something

Because /(.{1})/ will only match (and capture) one character between
two slashes (e.g. /u/, /b/) while /(.).*/ will match (and capture)
first character of word between two slashes (e.g. /unix/, /books/).


O< ascii ribbon campaign - stop html mail - www.asciiribbon.org

Thanks for your reply. I had previously read that section and enclosed
them in quotes and it didn’t work:
for example:

location ~* /manufacturers/ {
rewrite “^/manufacturers/(.{1})/(.*)$” /manufacturer/directory/$1
redirect;
}

doesn’t work. That;'s why I was so stumped. Thought I had done
everything according to that section…

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

Thank you for this explanation!

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs