Regex 使用位置中的id重写Nginx
我需要在nginx config中编写一个字符串,将Regex 使用位置中的id重写Nginx,regex,nginx,rewrite,Regex,Nginx,Rewrite,我需要在nginx config中编写一个字符串,将/user1.html重定向到/user/1-/。当然,ID应该类似于([0-9]+)。Donno为什么我不能 如果我写的时候没有regexprewrite^/user1.html$/user/1-/last,一切正常。但是当我添加regexp(rewrite^/user([0-9]+)\.html$/user/$1-/last;)时,nginx给了我404 除了([0-9]+),我还尝试编写(.*),但没有任何更改 我还试着为它写一个特殊的位
/user1.html
重定向到/user/1-/
。当然,ID应该类似于([0-9]+)。Donno为什么我不能
如果我写的时候没有regexprewrite^/user1.html$/user/1-/last代码>,一切正常。但是当我添加regexp(rewrite^/user([0-9]+)\.html$/user/$1-/last;
)时,nginx给了我404
除了([0-9]+)
,我还尝试编写(.*)
,但没有任何更改
我还试着为它写一个特殊的位置——也没有任何结果
请帮帮我
完整配置:
server {
listen 80;
server_name localhost;
root /var/www/localhost/;
index index.php;
if ($request_uri ~* "^(.*/)index\.php$") {
return 301 $1;
}
location / {
rewrite ^/user([0-9]+)\.html$ /user/\1-/ last;
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
使用\1
代替$1
如/user/\1-/
@AvinashRaj重写^/user([0-9]+)\.html$/user/\1-/last代码>?还没完成吗config@AlexeyTen将当前的一个添加到第一篇文章。首先,返回$1
,而不是\1
。第二,什么东西不起作用?为什么您认为nginx返回404,而不是您的PHP应用程序?