如何在Nginx中编写URL重写? [英] How to write a url rewrite in nginx?

查看:81
本文介绍了如何在Nginx中编写URL重写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望人们输入 http://www.myweb.com/like/1234456 将重定向到 http://www.myweb.com/item.php?itemid=1234456

I want people type in http://www.myweb.com/like/1234456 will redirect to http://www.myweb.com/item.php?itemid=1234456

我在配置中写了类似的东西,但是它不起作用.

I wrote something like this in the config but it doesn't work.

location = ^~/like/ {
                rewrite ^1234456  ../likeitem.php?item=1234456break;
                return 403;
        }

这只是一个测试.我还没有使用过$匹配.

this is just a test. I haven't used the $ matching yet.

我也重新启动了我的ngnix服务器,但仍然..它不执行重定向.

I also restart my ngnix server but still.. it doesn't do the redirect.

推荐答案

由于缺少$和对return命令的使用不当,以上代码无法正常工作.

The code above will not work because of a missing $ and poor use of the return command.

以下代码可与Nginx一起使用,包括0.8.54版.

The code below works with Nginx, including version 0.8.54.

下面的格式是:

  1. 所需URL
  2. 实际网址
  3. Nginx_Rule

它们必须在location / {}

http://example.com/notes/343
http://example.com/notes.php?id=343

rewrite ^/notes/(.*)$ /notes.php?id=$1 last;

http://example.com/users/BlackBenzKid
http://example.com/user.php?username=BlackBenzKid

rewrite ^/users/(.*)$ /user.php?username=$1 last;

http://example.com/top
http://example.com/top.php

rewrite ^/top?$ /top.php last;

更复杂

http://example.com/users/BlackBenzKid/gallery
http://example.com/user.php?username=BlackBenzKid&page=gallery

rewrite ^/users/(.*)/gallery$ /user.php?username=$1&page=gallery last;

这篇关于如何在Nginx中编写URL重写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆