nginx rewrite自定义url求解 150
1、实现如下url重写用户请求api.com/user.login?username=123&password=123时,自动重写到api.com/user.php?ac...
1、实现如下url重写
用户请求api.com/user.login?username=123&password=123时,
自动重写到api.com/user.php?act=login&username=123&password=123
2、nginx服务器配置如下,版本1.16.0
server
{
listen 80;
server_name api.com;
index index.html index.htm index.php default.html default.htm default.php;
root /www/api.com/api;
location / {
#rewrite ^/(.*)\.(.*)\?.*$ /$1\.php?act=$2&$query_string break;
#rewrite ^/(.*)\.(.*)\?.*$ /$1\.php?act=$2&$query_string last;
#rewrite (.*)\.(.*)$ $1\.php?act=$2&$query_string break;
#rewrite ^/(.*)\.(.*)$ /$1\.php?act=$2&$query_string break;
#rewrite ^/(.*)\.(.*)$ /$1\.php?act=$2&$query_string last;
#rewrite ^/(.*)\.(.*)$ /$1\.php?act=$2 last;
}
location ~ [^/]\.php(/|$)
{
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
location ~ /.well-known {
allow all;
}
location ~ /\.
{
deny all;
}
}
2.1、apache下如下写法是可以的
<Directory /www/api.com/api/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
Options All -Indexes
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteBase /
RewriteRule (.*)\.(.*)$ $1\.php?act=$2&%{QUERY_STRING} [L]
ErrorDocument 404 /404.html
</Directory>
3、一直报404错误或者直接提示下载user.login文件 展开
用户请求api.com/user.login?username=123&password=123时,
自动重写到api.com/user.php?act=login&username=123&password=123
2、nginx服务器配置如下,版本1.16.0
server
{
listen 80;
server_name api.com;
index index.html index.htm index.php default.html default.htm default.php;
root /www/api.com/api;
location / {
#rewrite ^/(.*)\.(.*)\?.*$ /$1\.php?act=$2&$query_string break;
#rewrite ^/(.*)\.(.*)\?.*$ /$1\.php?act=$2&$query_string last;
#rewrite (.*)\.(.*)$ $1\.php?act=$2&$query_string break;
#rewrite ^/(.*)\.(.*)$ /$1\.php?act=$2&$query_string break;
#rewrite ^/(.*)\.(.*)$ /$1\.php?act=$2&$query_string last;
#rewrite ^/(.*)\.(.*)$ /$1\.php?act=$2 last;
}
location ~ [^/]\.php(/|$)
{
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
location ~ /.well-known {
allow all;
}
location ~ /\.
{
deny all;
}
}
2.1、apache下如下写法是可以的
<Directory /www/api.com/api/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
Options All -Indexes
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteBase /
RewriteRule (.*)\.(.*)$ $1\.php?act=$2&%{QUERY_STRING} [L]
ErrorDocument 404 /404.html
</Directory>
3、一直报404错误或者直接提示下载user.login文件 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询