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

我尝试了很多不同的配置来启用任何以.php结尾的请求的永久重定向,以重定向到自己 .php。</ p>

问题是,我无法通过/index.php将请求重定向到任何目录重定向到/而不是/ index。</ p>

示例:</ p>

所需行为= /blog/index.php - &gt; / blog /
当前行为= /blog/index.php - &gt; / blog / index </ p>

是否有一个干净的解决方案,让任何包含“index.php”的请求将自己从请求中删除到简单的/,同时仍然从所有其他请求中删除.php 不包括index.php?</ p>

我无法按要求运行的两个问题行:</ p>

  if($ request_uri~  “^(。 /)index \ .php $”){return 301 $ 1;  } 
if($ request_uri~ ^ /(。*)\。php $){return 301 / $ 1; }
</ code> </ pre>

配置:</ p>

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

}

</ code> </ pre>
</ div>

展开原文

原文

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

}

duanduanxi9441
duanduanxi9441 我建议发布您目前/尝试过的内容
接近 5 年之前 回复

1个回答



您的浏览器可能会遇到以前的 301 Moved Permanently </ code>重定向到其缓存中的问题, 因此,你的一些较新的代码不会产生任何影响。</ p>

清除缓存,并尝试这样的事情:</ p>

  index index  .php; 
if($ request_uri~ ^ /([^?] ?)(?:(?&lt; = /)index(?:\ .php)?| \ .php)(\?。)? $){return 301 / $ 1 $ 2; }
</ code> </ pre>

上述行不仅支持删除 index.php </ code>,还支持只是 index </ code>,就像你之前的问题一样,以及 只是 .php </ code>,但它也保留了 $ args </ code>中可能的查询字符串。</ p>

为了避免支持剥离< 代码>索引</ code>,仍支持上述其余功能,请改用以下代码:</ p>

  if($ request_uri~ ^ /([^?] *  ?)(?:(?&lt; = /)index)?\ .php(\?。*)?$){return 301 / $ 1 $ 2;  } 
</代码> </ PRE>

P.S。 这个技巧的原始解释如下: nginx重定向循环 ,从网址中删除index.php 。</ p>
</ div>

展开原文

原文

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

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.

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. The original explanation of this trick is here: nginx redirect loop, remove index.php from url.

doumin1897
doumin1897 哇,这很完美。 谢谢。 我想我只是没有真正认识到你联系的解决方案,因为我在询问之前在搜索时实际上看到了它。
接近 5 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐