Mod rewrite 在nginx中为位置重写设置重写规则
我完全是nginx的不速之客。我正在尝试在nginx中为位置重写设置一些规则。我试图实现的是:Mod rewrite 在nginx中为位置重写设置重写规则,mod-rewrite,nginx,url-rewriting,Mod Rewrite,Nginx,Url Rewriting,我完全是nginx的不速之客。我正在尝试在nginx中为位置重写设置一些规则。我试图实现的是: 1. no change https://hello.domain.com/index.php --> https://hello.domain.com/index.php 2. subdomain needs to be included https://hello.domain.com --> https://hello.domain.com/index.php?sub=hello
1. no change
https://hello.domain.com/index.php --> https://hello.domain.com/index.php
2. subdomain needs to be included
https://hello.domain.com --> https://hello.domain.com/index.php?sub=hello
3. subdomain & folder structure needs to be included
https://hello.domain.com/dir1/15 --> https://hello.domain.com/index.php?sub=hello&path=dir1/15
4. requests to certain directories ( images, css, etc... ) need to be ignored
https://hello.domain.com/images/logo.png --> https://hello.domain.com/images/logo.png
我可以用这个函数将子域转换成变量$sub
server_name ~^(?<sub>.+)\.domain\.com$;
任何帮助都将不胜感激 这可能有助于您前进:
server_name ~^(?<sub>.+)\.domain\.com$;
location /images {
# You can put this code in a snippet file and than include
# it with e.g. `include assets.conf;` so you can use more
# folders with longest prefix match blocks, which are more
# efficient than regex blocks.
# Tell the browser to cache this for one day
expires 1d;
# Just give the resource
try_files $uri =404;
}
location /index.php {
# PHP stuff goes here
}
location / {
rewrite ^ /index.php?sub=$sub&path=$uri last;
}
server\u name~ ^(?.+)\.domain\.com$;
位置/图像{
#您可以将此代码放在代码段文件中,然后将其包括在内
#它使用例如“include assets.conf;”这样您就可以使用更多
#具有最长前缀匹配块的文件夹,其中
#比正则表达式块更有效。
#告诉浏览器将此缓存一天
过期1d;
#只需提供资源
try_files$uri=404;
}
location/index.php{
#PHP的东西在这里
}
地点/{
重写^/index.php?sub=$sub&path=$urilast;
}
您可以使用try_files
来处理案例4,请参阅以获取指导。或者可能是这样:如果($request_uri!~“^/(?:images | css)”{rewrite^”/index.php?path=$request_uri&sub=$sub“last;}
我一直在读到,如果不建议使用条件,这也适用于位置
范围,因为做了一个内部重定向
server_name ~^(?<sub>.+)\.domain\.com$;
location /images {
# You can put this code in a snippet file and than include
# it with e.g. `include assets.conf;` so you can use more
# folders with longest prefix match blocks, which are more
# efficient than regex blocks.
# Tell the browser to cache this for one day
expires 1d;
# Just give the resource
try_files $uri =404;
}
location /index.php {
# PHP stuff goes here
}
location / {
rewrite ^ /index.php?sub=$sub&path=$uri last;
}