doujiaohuo1096
2015-10-09 04:51
浏览 138
已采纳

nginx php友好的URL重定向,不会干扰index.php导致/ index

I've tried so many different configurations to enable permanent redirection of any request ending with .php to redirect to itself without .php.

The issue is, I can't get a rule to redirect requests made to any directory with /index.php to redirect to / instead of /index.

Example:

Desired behavior = /blog/index.php -> /blog/ Current behavior = /blog/index.php -> /blog/index

Is there a clean solution to have any request containing "index.php" to remove itself from the request to simply /, all while still removing .php from all other requests not including index.php?

The two problem lines I can't get to function as desired:

if ($request_uri ~* "^(.*/)index\.php$") { return 301 $1; }
if ($request_uri ~ ^/(.*)\.php$) { return 301 /$1; }

Config:

# 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;
}

}

图片转代码服务由CSDN问答提供 功能建议

我尝试了很多不同的配置来启用任何以.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; 
} 
 
}  
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题