用nginx重写URL

用nginx重写URL,nginx,url-rewriting,Nginx,Url Rewriting,我想用nginx重写url 样本: /something.php (not regular file) -> /index.php?site=something /somthingelse.php (regular file) -> /somethingelse.php 我当前的规则不起作用: location / { try_files $uri $uri/ @rules; } location @rules { rewrite ^/([a-z]*)\.php$

我想用nginx重写url

样本:

/something.php (not regular file) -> /index.php?site=something
/somthingelse.php (regular file) -> /somethingelse.php
我当前的规则不起作用:

location / {
    try_files $uri $uri/ @rules;
}

location @rules {
    rewrite ^/([a-z]*)\.php$ /index.php?s=$1;
}

你的
/something.php
url被
位置~\.php$
捕获,所以你需要在那里重写它

location ~ \.php$ {
    try_files $uri @rules;
    # usual php stuff
    ...;
}

location @rules {
    rewrite ^/([a-z]*)\.php$ /index.php?s=$1 last;
    return 404;
}
对于现有文件
/somefile.php
,它将像往常一样由php处理

对于不存在的
/other.php
它将被内部重定向到
@rules
,在那里它将被重写为
/index.php?s=other
,并再次进入
位置~\.php
,最后由php处理


对于不存在的
/w31rd.php
(其中
w31rd
[a-z]*
regexp不匹配)您将获得
404未找到
错误页面。

常规文件表示“存在”,非常规文件表示“不存在”?像虚拟文件位置?我也在找同样的东西。我猜你有类似于
location\.php$
@AlexeyTen:是的,我有