我正在尝试重写我的网址配置文件网址,使其干净且不包含参数,至少对用户而言。我正在使用nginx服务器,但在使其工作时遇到一些麻烦。
站点示例:http://website.com/subfolder/
个人资料链接:http://website.com/subfolder/profile.php
什么有效:http://website.com/subfolder/profile.php?username=Bobby
我想做什么:http://website.com/subfolder/Bobby
我的网站配置中的代码块:
location /subfolder/ {
if ($query_string ~ "^username=([A-Za-z0-9]+)$"){
rewrite ^/profile.php$ http://website.com/subfolder/%1 redirect;
}
}
您的重定向几乎是正确的。只是很小的外观变化。
location = /subfolder/profile.php {
if ($arg_username) {
return 301 /subfolder/$arg_username/;
}
fastcgi_pass 127.0.0.1:9001;
include fastcgi_params;
}
现在,regilero如何告诉,你需要在脚本中解析/subfolder/Username/,或者你可以通过nginx来完成。
location ~ ^/subfolder/(?<username>.+)/$ {
rewrite ^(.*)$ /subfolder/profile.php?username=$username break;
fastcgi_pass 127.0.0.1:9001;
include fastcgi_params;
fastcgi_param REQUEST_URI $uri$is_args$args;
}
2 重要说明:重写break
关键字防止重定向循环; fastcgi_param REQUEST_URI
通常是$request_uri
(fastcgi_params),但它是原始 URI,因此我们需要更改它。