【发布时间】:2016-01-06 22:08:06
【问题描述】:
我已经尝试了很多不同的配置来启用以 .php 结尾的任何请求的永久重定向,以重定向到没有 .php 的自身。
问题是,我无法获得将请求重定向到使用 /index.php 的任何目录以重定向到 / 而不是 /index 的规则。
例子:
期望的行为 = /blog/index.php -> /blog/ 当前行为 = /blog/index.php -> /blog/index
是否有一个干净的解决方案让任何包含“index.php”的请求从简单的/请求中删除自己,同时仍然从所有其他请求中删除 .php,不包括 index.php?
我无法按预期运行的两条问题线:
if ($request_uri ~* "^(.*/)index\.php$") { return 301 $1; }
if ($request_uri ~ ^/(.*)\.php$) { return 301 /$1; }
配置:
# Upstream
upstream backend {
server unix:/var/run/php5-fpm.sock;
}
server {
listen 443 ssl;
server_name mysite.net;
# Serving
root /var/www/html/mysite;
charset utf-8;
index index.php;
# Resources
location / {
try_files $uri $uri/ @extensionless-php;
}
location @extensionless-php {
rewrite ^(.*)$ $1.php last;
}
location ~* /includes/(.+)\.php$ {
deny all;
}
location ~ \.php {
try_files $uri =404;
fastcgi_pass backend;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# Status
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
}
【问题讨论】:
-
我建议发布您目前/迄今为止尝试过的内容