0

I'm trying to rewrite http://www.mydomain.com/xxxxx to http://www.mydomain.com/page1.php?h=xxxxx

I use this rule

rewrite /([a-z1-9]+)$ /page1.php?h=$1 last;

it redirect correctly to page1.php

but after clicking a link in (page1.php?h=xxxxx) to go to (page2.php?h=xxxxx)

it redirects to the same page (page1.php?h=xxxxx) and not enter (page2.php?h=xxxxx)

I don't know why, can any one help me?

thanks

Jim
  • 18,673
  • 5
  • 49
  • 65
Mas
  • 1,267
  • 1
  • 14
  • 13

1 Answers1

0

Apparently the below will not work due to nginx locations not allowing negative matching. (See: https://stackoverflow.com/a/16304073/398519 )

Ok if switching the setup is not an option than you will need a location, something like:

location !~* \.php$ {
    rewrite /([a-z1-9]+)$ /page1.php?h=$1 last;
}

I believe (not tested) that it should not match .php or any other item with a .ext and should only pull from the webroot portion. I am not able to test it at this time unfortunately.


For starters your initial rule is too vague. You need something to distinguish whats goes to page 1 and what goes to page 2.

Such as: http://www.domain.com/p1/xxxxxxx

That way you can parse at the p1 / p2 etc and know how to handle it. For completeness, here are some example rules:

rewrite /p1/([a-z1-9]+)$ /page1.php?h=$1 last;
rewrite /p2/([a-z1-9]+)$ /page2.php?h=$1 last;

If you want to use your original rule, then you need to add a file check and or change the regex to not match .php or .whatever I am not sure how to do this on nginx other than creating a location section before the root location so it gets trapped there first.

Community
  • 1
  • 1
Jim
  • 18,673
  • 5
  • 49
  • 65
  • ok thanks but I already have a script running and generating short links I just changed apache to nginx so rewrite rules also changed, so I just want the rule to transfer the hash when linked to the domain only – Mas Nov 13 '13 at 16:08
  • @Mas Just updated it with a location rule, hopefully that works. – Jim Nov 13 '13 at 16:14
  • @Mas, ok, I made it a bit simpler, try that. It should redirect anything that does not have a .php after it. – Jim Nov 13 '13 at 16:24
  • it gives me this message (nginx: [emerg] invalid location modifier "!~*" in /etc/nginx/conf.d/default.conf:50) @Brad F Jacobs – Mas Nov 13 '13 at 16:27