Как скрывать расширения .html и .php и редиректить на url без них на nginx?
Как скрывать расширения .html и .php и редиректить на url без них п помощью nginx?
То есть при переходе на api.example.com/method/servers.get.php?ids=[2] нужно перенапрявлять на api.example.com/method/servers.get?ids=[2].
Есть уже готовый конфиг:
server_name api.example.com ;
listen *.*.*.*:80;
listen [*.*.*.*.*]:80;
listen *.*.*.*:443 ssl ;
listen [*.*.*.*.*]:443 ssl ;
ssl_certificate "/var/www/httpd-cert/api.example.com_*-*-*-*-*_*.crt";
ssl_certificate_key "/var/www/httpd-cert/api.example.com_*-*-*-*-*_*.key";
charset utf-8;
gzip on;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/css text/xml application/javascript text/plain application/json image/svg+xml image/x-icon;
gzip_comp_level 1;
set $root_path /var/www/*/data/www/api.example.com;
root $root_path;
disable_symlinks if_not_owner from=$root_path;
rewrite index.php https://example.com last;
rewrite ^/method/(.+)$ /public/$1 last;
rewrite ^/public/(.+)$ https://api.example.com/method/$1 last;
location ~* ^/(vendor)($|\/) {
deny all;
}
location / {
index index.php index.html;
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/api.bebra.host.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
}
location ~* ^.+\.(jpg|jpeg|gif|png|svg|js|css|mp3|ogg|mpeg|avi|zip|gz|bz2|rar|swf|ico|7z|doc|docx|map|ogg|otf|pdf|tff|tif|txt|wav|webp|woff|woff2|xls|xlsx|xml)$ {
try_files $uri $uri/ /index.php?$args;
}
location @fallback {
fastcgi_pass unix:/var/run/api.example.com.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
include "/etc/nginx/fastpanel2-sites/*/api.example.com.includes";
include /etc/nginx/fastpanel2-includes/*.conf;
error_log /var/www/*/data/logs/api.example.com-frontend.error.log;
access_log /var/www/*/data/logs/api.example.com-frontend.access.log;
}
Помогите, пожалуйста, исправить его.
Ответы (1 шт):
Автор решения: Aleksey Vaganov
→ Ссылка
Для 301 редиректа с отбрасыванием .html и .php добавьте в конфиг перед location ~ \.php$:
location ~ ^(.*)\.(?:html|php)$ {
return 301 $1$is_args$args;
}
Но обращаю ваше внимание, что при этом location ~ \.php$ уже не будет отрабатывать.