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

4
  • aren't you always rewriting to page1.php??? Commented Nov 13, 2013 at 15:55
  • yes, so what can I do Commented Nov 13, 2013 at 15:58
  • but when in page1.php I don't want to run the rewrite rule Commented Nov 13, 2013 at 15:59
  • @Mas: I would take a look at this answer: stackoverflow.com/a/16304073/398519 Commented Nov 13, 2013 at 16:41

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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 Just updated it with a location rule, hopefully that works.
@Mas, ok, I made it a bit simpler, try that. It should redirect anything that does not have a .php after it.
it gives me this message (nginx: [emerg] invalid location modifier "!~*" in /etc/nginx/conf.d/default.conf:50) @Brad F Jacobs

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.