根目录内的文件无法访问 mod_rewrite PHP

发布于 2024-12-23 13:47:04 字数 539 浏览 1 评论 0 原文

文件结构看起来像这样

  • root
  • root/x
  • root/x/y.php
  • root/index.php

.htaccess 代码

RewriteRule (.*) /x/$1 [L]

htaccess 文件的作用是从 url 中删除文件夹名称 (x),以便访问 y.php = http://localhost/y.php 而不是 http://localhost/x/y.php 。这可行,但我现在的问题是index.php 显示如下内容:

Index of /x

Parent Directory
y.php

我无法访问index.php。我相信 x 成为根文件夹。 感谢您的帮助!

The file structure looks like this

  • root
  • root/x
  • root/x/y.php
  • root/index.php

.htaccess code

RewriteRule (.*) /x/$1 [L]

What the htaccess file does is to remove the folder name (x) from the url so accessing y.php = http://localhost/y.php instead of http://localhost/x/y.php . This works but my problem now is the index.php shows something like this:

Index of /x

Parent Directory
y.php

I can't access the index.php. I believe the x became the root folder.
Thanks for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深陷 2024-12-30 13:47:04

首先,您不应该允许在公共服务器上像这样显示索引...在您的本地主机上可能没问题,但是您仍然可以通过添加以下内容在 .htaccess 中禁用它:

Options -Indexes

要解决您的问题,您可能应该添加在重写规则之前满足以下条件,以便该规则不会应用于磁盘上实际存在的任何文件或目录:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

这不会解决您遇到的问题,因为 .htaccess 正在重定向到不存在的 / x/index.php文件...这就是为什么它显示 /x 目录的索引列表

First, you shouldn't allow index displays like this on a public server ... on your localhost it may be okay, but still, you can disable it in your .htaccess by adding this:

Options -Indexes

To address your question, you should probably add the following conditions before your rewrite rule so that the rule won't apply to any files or directories that actually exist on disk:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

This won't fix the issue you have because the .htaccess is redirecting to a non-existant /x/index.php file ... this is why it's showing the index listing for the /x directory

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文