Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx rewrite WITHOUT change url

Tags:

nginx

rewrite

I want to use rewrite function in my nginx server.

When I try "http://www.example.com/1234", I want to rewrite "http://www.example.com/v.php?id=1234" and want to get "http://www.example.com/1234" in browser.

Here is nginx.conf file

...   location ~ /[0-9]+ {       rewrite "/([0-9]+)" http://www.example.com/v.php?id=$1 break;   } ... 

When I try "http://www.example.com/1234"

I want to ...

url bar in browser : http://www.example.com/1234 real url : http://www.example.com/v.php?id=1234 

but I'm in trouble ...

url bar in browser : http://www.example.com/v.php?id=1234 real url : http://www.example.com/v.php?id=1234 
like image 824
user1850593 Avatar asked Mar 10 '13 13:03

user1850593


People also ask

How do you write rewrite rules in NGINX?

The syntax of rewrite directive is: rewrite regex replacement-url [flag]; regex: The PCRE based regular expression that will be used to match against incoming request URI. replacement-url: If the regular expression matches against the requested URI then the replacement string is used to change the requested URI.

What is return 301 in NGINX?

Permanent redirects such as NGINX 301 Redirect simply makes the browser forget the old address entirely and prevents it from attempting to access that address anymore. These redirects are very useful if your content has been permanently moved to a new location, like when you change domain names or servers.

What does $Uri mean in NGINX?

It is exactly the $uri/ part that makes nginx assuming an URI can be a directory name and looking for an index file presence inside it.


1 Answers

Reference: http://wiki.nginx.org/HttpRewriteModule#rewrite

If the replacement string begins with http:// then the client will be redirected, and any further >rewrite directives are terminated.

So remove the http:// part and it should work:

location ~ /[0-9]+ {         rewrite "/([0-9]+)" /v.php?id=$1 break; } 
like image 99
Chuan Ma Avatar answered Oct 04 '22 05:10

Chuan Ma