我尝试了很多不同的配置来启用任何以 .php 结尾的请求的永久重定向,以重定向到不带 .php 的自身。
问题是,我无法获得将使用 /index.php 发送到任何目录的请求重定向到 / 而不是 /index 的规则。
Example:
期望的行为 = /blog/index.php -> /blog/
当前行为 = /blog/index.php -> /blog/index
是否有一个干净的解决方案可以让任何包含“index.php”的请求将其自身从简单/的请求中删除,同时仍然从不包括index.php的所有其他请求中删除.php?
我无法按预期运行的两个问题行:
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;
}
}
您的浏览器可能会遇到问题301 Moved Permanently
在其缓存中重定向,因此您的一些较新的代码不会产生任何效果。
清除缓存,然后尝试以下操作:
index index.php;
if ($request_uri ~ ^/([^?]*?)(?:(?<=/)index(?:\.php)?|\.php)(\?.*)?$) { return 301 /$1$2; }
上面的行不仅支持剥离index.php
, 但是也just index正如您从之前的问题中可能了解到的那样 https://stackoverflow.com/questions/32962965/nginx-redirect-php-uris-to-friendly-counterpart/32965758#32965758,以及刚刚.php
,但它保留了可能的查询字符串$args
, too.
为了避免仅仅支持剥离index
,仍然支持上述其余功能,请改用以下内容:
if ($request_uri ~ ^/([^?]*?)(?:(?<=/)index)?\.php(\?.*)?$) { return 301 /$1$2; }
附:这个技巧的原始解释在这里:nginx 重定向循环,从 url 中删除 index.php https://stackoverflow.com/questions/21687288/nginx-redirect-loop-remove-index-php-from-url/21813759#21813759.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)