0

I want to check if a parameter is present in a url in nginx and then rewrite.

How can i do that?

For e.g if url is http://website.com/search/node/item123/loc=33 then redirect to http://website.com/search/node/item123

my code is here and not work !

if ($arg_loc != "") { rewrite ^(/search/.*)$ $1? permanent; }

UPDATE : i want redirect any url that ends with loc=number to the parent url ! for arabic and enlgish characters

EG :

http://website.com/search/node/item123/loc=33 

i want redirect to

http://website.com/search/node/item123/ 

or

http://website.com/search/node/عربیک/loc=55 

should be :

http://website.com/search/node/عربیک 
6
  • 1
    Your example URL has no query arguments, therefore your if code cannot work. Commented Nov 10, 2023 at 21:49
  • Are you sure there's no question mark in the first URL? Commented Nov 11, 2023 at 12:58
  • No Bro !! any question mark Commented Nov 11, 2023 at 13:04
  • Please clarify your issue. Does the URL you want to redirect contain query arguments or not? If it has query arguments, then your if approach is correct. If not, then you need to match on the URL path. Commented Nov 11, 2023 at 17:49
  • In that case it's no a parameter, but a some weird URL. Commented Nov 11, 2023 at 17:51

1 Answer 1

1

There is no query parameters in your examples. It's just an url, so you could use rewrite.

Something like that:

rewrite "^(/search/node/[^/]+)/loc=\d+$" $1 permanent; 

P.S. I would strongly recommend to use keyword redirect instead of permanent until you're 1000% sure that redirect works as it should.

5
  • How can i use keyword redirect instead of permanent ? Commented Nov 12, 2023 at 17:32
  • @BijanZand, what do you mean? Just put redirect instead of permanent. This will do 302 redirect instead of 301. The problem with permanent is, well, it's permanent and if you make a mistake you'll have hard time to remove that permanent redirect from your browser. Commented Nov 12, 2023 at 17:47
  • @BijanZand well \d stand for digits. You'll need \w or [a-z0-9] I guess. Anyway, make yourself familiar with regular expressions. Commented Nov 12, 2023 at 18:08
  • i see your update man , but for mix character Arabic & English character \w+ not working ! do you have a solution? Commented Nov 12, 2023 at 18:21
  • @BijanZand you really should have been mention all these conditions in original question. I've updated answer. Commented Nov 12, 2023 at 18:28

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.