基于nginx搭建的WEB服务器,怎么弄rewrite?

2016-07-07 15:19 来源:www.chinab4c.com 作者:ecshop专家

好像discuz官方站点也是用的nginx吧,请问用nginx的话,怎么去设置rewirte来让ecshop能够使用rewrite功能呢?

回答:
nginx的rewrite要写到相应的虚拟主机当中

例:
  1. if (!-e $request_filename)
  2. {
  3. rewrite ^index\.html$ index\.php;
  4. rewrite ^category$ index\.php;
  5. rewrite ^feed-c([0-9]+)\.xml$ feed\.php\?cat=$1;
  6. rewrite ^feed-b([0-9]+)\.xml$ feed\.php\?brand=$1;
  7. rewrite ^feed\.xml$ feed\.php;
  8. rewrite ^category-([0-9]+)-b([0-9]+)-min([0-9]+)-max([0-9]+)-attr([^-]*)-([0-9]+)-(.+)-([a-zA-Z]+)(.*)\.html$category\.php\?id=$1&brand=$2&price_min=$3&price_max=$4&filter_attr=$5&page=$6&sort=$7&order=$8;
  9. rewrite ^category-([0-9]+)-b([0-9]+)-min([0-9]+)-max([0-9]+)-attr([^-]*)(.*)\.html$ category\.php\?id=$1&brand=$2&price_min=$3&price_max=$4&filter_attr=$5;
  10. rewrite ^category-([0-9]+)-b([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)(.*)\.html$ category\.php\?id=$1&brand=$2&page=$3&sort=$4&order=$5;
  11. rewrite ^category-([0-9]+)-b([0-9]+)-([0-9]+)(.*)\.html$ category\.php\?id=$1&brand=$2&page=$3;
  12. rewrite ^category-([0-9]+)-b([0-9]+)(.*)\.html$category\.php\?id=$1&brand=$2;
  13. rewrite ^category-([0-9]+)(.*)\.html$ category\.php\?id=$1;
  14. rewrite ^goods-([0-9]+)(.*)\.html$goods\.php\?id=$1;
  15. rewrite ^article_cat-([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)(.*)\.html$article_cat\.php\?id=$1&page=$2&sort=$3&order=$4;
  16. rewrite ^article_cat-([0-9]+)-([0-9]+)(.*)\.html$ article_cat\.php\?id=$1&page=$2;
  17. rewrite ^article_cat-([0-9]+)(.*)\.html$ article_cat\.php\?id=$1;
  18. rewrite ^article-([0-9]+)(.*)\.html$ article\.php\?id=$1;
  19. rewrite ^brand-([0-9]+)-c([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)\.html brand\.php\?id=$1&cat=$2&page=$3&sort=$4&order=$5;
  20. rewrite ^brand-([0-9]+)-c([0-9]+)-([0-9]+)(.*)\.html brand\.php\?id=$1&cat=$2&page=$3;
  21. rewrite ^brand-([0-9]+)-c([0-9]+)(.*)\.html brand\.php\?id=$1&cat=$2;
  22. rewrite ^brand-([0-9]+)(.*)\.html brand\.php\?id=$1;
  23. rewrite ^tag-(.*)\.html search\.php\?keywords=$1;
  24. rewrite ^snatch-([0-9]+)\.html$ snatch\.php\?id=$1;
  25. rewrite ^group_buy-([0-9]+)\.html$ group_buy\.php\?act=view&id=$1;
  26. rewrite ^auction-([0-9]+)\.html$ auction\.php\?act=view&id=$1;
  27. }
复制代码


没做详细测试,可以先试试。

楼上的好像懂很多.崇拜.

高手就是高手。

以上明显不对,好像适用于Apache 规则的, 应该用下面这个:

location ~* ^.+\.(bak|inc|lib|sh|tpl|lbi|dwt)$
{
deny all;
}

location /

{
rewrite "^(.*)/index.html$" $1/index.php last;
rewrite "^(.*)/category$" $1/index.php last;

# access any object by its numeric identifier
rewrite "^(.*)/feed-c([0-9]+).xml$" $1/feed.php?cat=$2 last;
rewrite "^(.*)/feed-b([0-9]+).xml$" $1/feed.php?brand=$2 last;
rewrite "^(.*)/feed.xml$" $1/feed.php last;

rewrite "^(.*)/category-([0-9]+)-b([0-9]+)-min([0-9]+)-max([0-9]+)-attr([^(.*)/-]*)-([0-9]+)-(.+)-([a-zA-Z]+)(.*).html$" $1/category.php?id=$2&brand=$3&price_min=$4&price_max=$5&filter_attr=$6&page=$7&sort=$8&order=$9 last;
rewrite "^(.*)/category-([0-9]+)-b([0-9]+)-min([0-9]+)-max([0-9]+)-attr([^(.*)/-]*)(.*).html$" $1/category.php?id=$2&brand=$3&price_min=$4&price_max=$5&filter_attr=$6 last;
rewrite "^(.*)/category-([0-9]+)-b([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)(.*).html$" $1/category.php?id=$2&brand=$3&page=$4&sort=$5&order=$6 last;
rewrite "^(.*)/category-([0-9]+)-b([0-9]+)-([0-9]+)(.*).html$" $1/category.php?id=$2&brand=$3&page=$4 last;
rewrite "^(.*)/category-([0-9]+)-b([0-9]+)(.*).html$" $1/category.php?id=$2&brand=$3 last;
rewrite "^(.*)/category-([0-9]+)(.*).html$" $1/category.php?id=$2 last;

rewrite "^(.*)/goods-([0-9]+)(.*).html$" $1/goods.php?id=$2 last;

rewrite "^(.*)/article_cat-([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)(.*).html$" $1/article_cat.php?id=$2&page=$3&sort=$4&order=$5 last;
rewrite "^(.*)/article_cat-([0-9]+)-([0-9]+)(.*).html$" $1/article_cat.php?id=$2&page=$3 last;
rewrite "^(.*)/article_cat-([0-9]+)(.*).html$" $1/article_cat.php?id=$2 last;
rewrite "^(.*)/article-([0-9]+)(.*).html$" $1/article.php?id=$2 last;

rewrite "^(.*)/brand-([0-9]+)-c([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+).html$" $1/brand.php?id=$2&cat=$2&page=$4&sort=$5&order=$6 last;
rewrite "^(.*)/brand-([0-9]+)-c([0-9]+)-([0-9]+)(.*).html$" $1/brand.php?id=$2&cat=$3&page=$4 last;
rewrite "^(.*)/brand-([0-9]+)-c([0-9]+)(.*).html$" $1/brand.php?id=$2&cat=$3 last;
rewrite "^(.*)/brand-([0-9]+)(.*).html$" $1/brand.php?id=$2 last;

rewrite "^(.*)/tag-(.*).html$" $1/search.php?keywords=$2 last;
rewrite "^(.*)/snatch-([0-9]+).html$" $1/snatch.php?id=$2 last;
rewrite "^(.*)/group_buy-([0-9]+).html$" $1/group_buy.php?act=view&id=$2 last;
rewrite "^(.*)/auction-([0-9]+).html$" $1/auction.php?act=view&id=$2 last;
}



www.linyufang.net


他那个是nginx的规则 ~~~

晓天正解

听人说,Rewrite时把数字的东西放在最后有利于SEO,特别是英文站,所以我重写了Rewrite规则,把所有的ID都放到了最后面。
例如:
有商品名为(耐克衬衣),在库中编号为200:Nike Shirt
没有Rewrite时,访问地址为:http://www.***.com/goods.php?id=200
默认复杂Rewrite的URL为:http://www.***.com/goods-200-Nike+Shirt.html

修改Rewirte后的访问URL为:http://www.***.com/goods-Nike-Shirt-200.html
(并且改默认URLEncode空格为+号,变为-号)。

演示地址:http://www.googoow.com(本站纯萃测试ecshop2.7用,没有添加任何商品(保留默认的),非赢利。)。

6# 晓天

你在Nginx上测试过了么?????我这里用的是nginx/0.7.61

MARK.....






哈..哥们..我顶你...

2楼的确实不能用..其他人不知道能不能用..反正我是用了不..我用的和你的差不多..所以.. 要下结论还得实践了才有说服力.. 我的是 nginx/0.7.63

我试了,你的可以用..

不过我加这段的话..会报错..

location ~* ^.+\.(bak|inc|lib|sh|tpl|lbi|dwt)$
{
deny all;
}

所以,,我还是用我的吧...路过..顶你一下..呵..

404 Not Found

404 Not Found

--------------------------------------------------------------------------------

nginx/1.0.0