nginx子域重写

发布于 2024-08-26 08:46:08 字数 286 浏览 1 评论 0原文

我需要一个 nginx 重写规则来重写:

http://some-keyword.example.comwww.example.com/keyword.php?keyword=$some-keyword

而前面没有 www 的域名仍会重写为 www.example.com 并且 www 不会被视为关键字。

请你帮我解决这个问题,这两条规则如何写?

I need a nginx rewrite rule to rewrite from:

http://some-keyword.example.com to www.example.com/keyword.php?keyword=$some-keyword

while domain without www in front still rewrites to www.example.com and www isn't taken as a keyword.

Please could you help me to solve this problem, how to write these two rules?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

娇纵 2024-09-02 08:46:08

如果您指的是重定向,那么:

server {
  server_name ~^(.*)\.example\.com$ ;

  rewrite ^ http://www.example.com/keyword.php?keyword=$1 redirect;
}

重写的情况下,只需执行以下操作即可

server {
  server_name example.com ~^(.*)\.example\.com$ ;

  rewrite ^ /keyword.php?keyword=$1 break;

#  location /keyword.php {
#    ....
#  }
}

If you meant redirect, then:

server {
  server_name ~^(.*)\.example\.com$ ;

  rewrite ^ http://www.example.com/keyword.php?keyword=$1 redirect;
}

In the case of rewrite then simply do

server {
  server_name example.com ~^(.*)\.example\.com$ ;

  rewrite ^ /keyword.php?keyword=$1 break;

#  location /keyword.php {
#    ....
#  }
}
独行侠 2024-09-02 08:46:08
rewrite ^/list-c-([0-9]+)-k-(.+)-o-1\.html$ /index.php?module=Default&action=List&c=$1&k=$2&o=1 last;
rewrite ^/list-c-([0-9]+)-k-(.+)-o-1-p-(.+)\.html$ /index.php?module=Default&action=List&c=$1&k=$2&o=1&p=$3 last;
rewrite ^/list-c-([0-9]+)-k-(.+)-o-1-price-(.+)\.html$ /index.php?module=Default&action=List&c=$1&k=$2&o=1&price=$3 last;
rewrite ^/list-c-([0-9]+)-k-(.+)-o-(.+)-p-1\.html$ /index.php?module=Default&action=List&c=$1&k=$2&o=$3&p=1 last;
rewrite ^/list-c-([0-9]+)-k-(.+)-o-(.+)-p-(.+)\.html$ /index.php?module=Default&action=List&c=$1&k=$2&o=$3&p=$4 last;
rewrite ^/list-c-([0-9]+)-k-(.+)-o-1\.html$ /index.php?module=Default&action=List&c=$1&k=$2&o=1 last;
rewrite ^/list-c-([0-9]+)-k-(.+)-o-1-p-(.+)\.html$ /index.php?module=Default&action=List&c=$1&k=$2&o=1&p=$3 last;
rewrite ^/list-c-([0-9]+)-k-(.+)-o-1-price-(.+)\.html$ /index.php?module=Default&action=List&c=$1&k=$2&o=1&price=$3 last;
rewrite ^/list-c-([0-9]+)-k-(.+)-o-(.+)-p-1\.html$ /index.php?module=Default&action=List&c=$1&k=$2&o=$3&p=1 last;
rewrite ^/list-c-([0-9]+)-k-(.+)-o-(.+)-p-(.+)\.html$ /index.php?module=Default&action=List&c=$1&k=$2&o=$3&p=$4 last;
拥抱没勇气 2024-09-02 08:46:08

如果可能的话,我只创建 1 个服务器(虚拟主机),即普通的 domain.com/www.domain.com,然后使用 conf 重写其余部分 他们。

server {
    server_name domain.com www.domain.com;
    # normal handling for files
}

server {
    server_name ~(?<subdomain>[^\.]*).domain.com;
    location / {
        try_files keyword.php?keyword=$subdomain =404;
    }
}

如果我错过了什么,请告诉

If it's possible I'd only create 1 server(virtual host) which is the normal domain.com/www.domain.com and then use the conf to rewrite the rest to them

server {
    server_name domain.com www.domain.com;
    # normal handling for files
}

server {
    server_name ~(?<subdomain>[^\.]*).domain.com;
    location / {
        try_files keyword.php?keyword=$subdomain =404;
    }
}

please tell me if I missed something.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文