How to rewrite these

Hi Nginx,

I’m migrating my website into onother application. I need to rewrite
some of
these

http://www.mydomain.com/category/subcategory

into

http://www.mydomain.com/application/category/subcategory
application/category/subcategory

I need to insert /application before my category name

Thank’s before

On Fri, Feb 19, 2010 at 03:11:09PM +0700, Glen L. wrote:

http://www.mydomain.com/application/category/subcategory

I need to insert /application before my category name

If you use proxying, then:

location / {
    proxy_pass   http://backend/application/;
}


Igor S.
http://sysoev.ru/en/

No i’m not proxying…

Because all I need is to rewrite arround 50 that category

On Fri, Feb 19, 2010 at 05:02:44PM +0700, Glen L. wrote:

No i’m not proxying…

What do you use ?

Because all I need is to rewrite arround 50 that category

Could you show some examples ?

I’m migrating my website into onother application. I need to rewrite some
I need to insert /application before my category name
Igor Sysoev
nginx Info Page

Igor S.
Igor Sysoev

Example

Old : http://www.mydomain.com/breaking-news
New : http://www.mydomain.com/category/breaking-news

Old : http://www.mydomain.com/national
New : http://www.mydomain.com/category/national

Old : http://www.mydomain.com/international
New : http://www.mydomain.com/category/international

I’m using php-fpm for these

Is that posibble to use somethink like

 location ~ ^/breaking-news|national|...) {
rewrite ^ /category$request_uri
 }

?

On Fri, Feb 19, 2010 at 05:11:14PM +0700, Glen L. wrote:

I’m using php-fpm for these

Something like this:

 location ~ ^/breaking-news|national|...) {
     fastcgi_pass   ...
     fastcgi_parm   SCRIPT_FILENAME  /path/to/scripts/category$uri;
     ...
 }

What do you use ?

    proxy_pass   http://backend/application/;

nginx Info Page


nginx mailing list
[email protected]
nginx Info Page


nginx mailing list
[email protected]
nginx Info Page


Igor S.
http://sysoev.ru/en/

On Fri, Feb 19, 2010 at 05:20:20PM +0700, Glen L. wrote:

Is that posibble to use somethink like

 location ~ ^/breaking-news|national|...) {
rewrite ^ /category$request_uri      
  • rewrite ^ /category$request_uri;
    
  • rewrite ^ /category$uri;
 }

?

Possible, but why to use rewrite, if you may pass request directly where
you want ? In my opinion rewrites cause spaghetti configuration.

Old : http://www.mydomain.com/breaking-news
Something like this:
To: [email protected]
Could you show some examples ?

http://www.mydomain.com/application/category/subcategory

nginx mailing list
nginx Info Page


nginx mailing list
[email protected]
nginx Info Page


nginx mailing list
[email protected]
nginx Info Page


Igor S.
http://sysoev.ru/en/

I prefer to rewrite it, because what we concerned is google indexing…
sometimes google will remove the old address, and getting the new one
So that when people accessing our site, they will bookmark the new
address

Bump…

On Sat, Feb 20, 2010 at 8:17 PM, Glen L. [email protected]
wrote:

Bump…

you should start learning writing rewrite rules…

rewrite ^/news/(.+)/title
http://app.mydomain.com/redirect/application/$1 permanent;

OK thank’s for ur suggestion… It’s working like a charm… 1 more
question

I have a url http://www.mydomain.com/news/$id/title

How can I redirect it to
http://app.mydomain.com/redirect/application/$id ?

On Sat, Feb 20, 2010 at 8:45 PM, Glen L. [email protected]
wrote:

Is that posibble to detect if (.+) is numbers only?

yes.

the method should be mentioned in any guide about regular expressions.
Which includes this [1] and this [2].

[1] PCRE Regular Expression Pattern Syntax Refference (PHP preg*)

[2] Regular Expressions Reference


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

Is that posibble to detect if (.+) is numbers only?

So if http://www.mydomain.com/news/1231245/title
Then rewrite to http://app.mydomain.com/redirect/application/1231245

If http://www.mydomain.com/news/category/title
Then do nothing… just bypass to the content

Thank’s for your prompt reply…

However i tried this

rewrite “/news/([0-9] {2})([0-9] {2})([0-9] {2})/$”
http://app.mydomain.com/redirect/application/$1$2$3;

but it doesn’t work. What I’ve missed?

the spaces: “] {” -> “]{”.

may i ask why you catch three times two numbers instead of one time six?

Glen L. a écrit :

Thank’s for your prompt reply…

However i tried this

rewrite “/news/([0-9] {2})([0-9] {2})([0-9] {2})/$”
http://app.mydomain.com/redirect/application/$1$2$3;

but it doesn’t work. What I’ve missed?

The regexp pattern isn’t correct. You shouldn’t have spaces before
the “{2}” unless you really want to match two consecutive spaces.

Even without those spaces, this pattern would match urls like
“/news/123456/” (without “/title” and with a trailing slash), while
you said you need to match “/news/$id/title” instead. Also, if you
want to fade out the old url in the long run, you could use a
permanent redirect.

You probably want something like that:
rewrite ^/news/([0-9]{6})/title$
http://app.mydomain.com/redirect/application/$1 permanent;

The spaces fixed the problem :slight_smile:

By the way, if i have only 1, 2, 3, 4, 5 digits of numbers, it’s not
redirected

honestly, i think you should really go through some regex tutorials

It means that if I have 6 digits of numbers, it will redirect to the url
i’ve described

But if I have 5 digits of numbers, it’s not redirecting to the URL.