nginx php友好的URL重定向而不会干扰index.php导致/index [英] nginx php friendly URL redirection without interfering with index.php causing /index

查看:76
本文介绍了nginx php友好的URL重定向而不会干扰index.php导致/index的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了许多不同的配置,以使以.php结尾的任何请求能够永久重定向,而无需.php即可重定向到自身.

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

问题是,我无法使用规则将使用/index.php重定向到任何目录的请求重定向到/而不是/index.

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.

示例:

所需行为=/blog/index.php->/blog/ 当前行为=/blog/index.php->/blog/index

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

是否有一种干净的解决方案,可以将任何包含"index.php"的请求从请求中删除,使其简单地/,同时仍从不包括index.php的所有其他请求中删除.php?

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

配置:

# 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重定向到其缓存中时,您可能会遇到此问题,因此某些新代码不会起作用

You're likely experiencing the issue with your browser having your prior 301 Moved Permanently redirects in its cache, thus some of your newer code would not be having any effect.

清除缓存,然后尝试执行以下操作:

Clear the cache, and try something like this:

index index.php;
if ($request_uri ~ ^/([^?]*?)(?:(?<=/)index(?:\.php)?|\.php)(\?.*)?$) { return 301 /$1$2;   }

以上代码行不仅支持剥离index.php,还剥离

The above line supports stripping out not only index.php, but also just index as you might have from your earlier question, as well as just .php, yet it preserves the possible query string from $args, too.

为避免仅支持剥离index而仍支持上述其余功能,请改用以下内容:

To avoid having support for stripping just index, still supporting the rest of the features as above, use the following instead:

if ($request_uri ~ ^/([^?]*?)(?:(?<=/)index)?\.php(\?.*)?$) {   return 301 /$1$2;   }

P.S.此技巧的原始解释在这里: nginx重定向循环,从网址中删除index.php .

P.S. The original explanation of this trick is here: nginx redirect loop, remove index.php from url.

这篇关于nginx php友好的URL重定向而不会干扰index.php导致/index的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆