【问题标题】:apache .htaccess to nginx rewrite ruleapache .htaccess 到 nginx 重写规则
【发布时间】:2014-04-24 16:02:35
【问题描述】:

我需要从 apache 更改为 Nginx,但 .htaccess 在 Nginx 服务器上不起作用。

我在我的 Apache .htaccess 中得到了关注:

RewriteEngine On 

# always run through the indexfile
RewriteRule .$ index.php

# don't let people peek into directories without an index file
Options -Indexes

当我在 Nginx 服务器上放置 .htaccess 时,该文件被服务器删除。 我必须将.htaccess 数据放在哪种文件中(请提供名称和后缀)以及如何为 Nginx 重写它以及将文件放在哪里?

我希望所有子目录文件都通过我的index.php 文件,这样我就可以包含我的index.php 文件中的特定文件...

我尝试过类似的方法:

# nginx configuration

autoindex off;

location / {
    rewrite .$ /index.php;
}

但它不起作用。该目录如下所示:

── root
    ├── folder1
    │   └── folder2
    │       └── file.php
    └── index.php

所以如果我要求 root/folder1/folder2/file.php 我希望服务器加载 root/index.php 因为我仍然有服务器 url 请求, 我可以从我的index.php 中包含root/folder1/folder2/file.php

这一切都适用于我的 apache 安装,但不适用于 Nginx 服务器。 请帮帮我。

#

编辑: 原来nginx.conf 文件必须位于根文件夹上方,并且只有在您有权访问根文件夹上方的文件夹时才能访问。像服务器管理员访问是必要的。

【问题讨论】:

  • 您找到anilcetin.comwinginx.com/en/htaccess了吗?
  • 是的和许多其他网站。我尝试将我的代码放在同一文件夹中的 nginx.conf 和 nginx.conf.txt 中,但不起作用。 nginx 服务器会自动删除 htaccess 文件。代码要放在 htaccess 文件中吗?

标签: php apache .htaccess mod-rewrite nginx


【解决方案1】:

Nginx 没有 htaccess 文件。代码需要进入 nginx 配置文件。另外,尝试在重写中添加“last”标志:

# nginx configuration

autoindex off;

location / {
    rewrite .* /index.php last;
}

【讨论】:

  • 该文件是否会被称为 nginx.conf 并在 index.php 的根文件夹中?
  • @user3434534 编号See the third paragraph in the beginner's guide which tells you where it is。在不同的系统上是不同的。
  • 如果我无权访问根目录,是否可以用我正在使用的目录中放置自己的 nginx.conf 文件来覆盖它?
  • @Medda86 不,这不是 nginx 的工作方式。如果您无权访问配置文件,则无法更改 nginx 的行为。
  • 好的,谢谢,我将不得不与管理人员交谈,然后为我设置它。感谢您的解释。
猜你喜欢
  • 2014-06-30
  • 2016-08-09
  • 2020-01-10
  • 1970-01-01
  • 2017-07-26
  • 2018-05-05
  • 1970-01-01
  • 1970-01-01
  • 2023-03-18
相关资源
最近更新 更多