简体   繁体   English

htaccess RewriteEngine导致子目录问题

[英]htaccess RewriteEngine causing subdirectory issues

Quick question for you guys. 你们的快速问题。 My website's basic file structure is as follows: 我的网站的基本文件结构如下:

www.example.com www.example.com

/ /

index.php 的index.php

otherFiles.php otherFiles.php

./directory 。/目录

  index.php 

Currently if you were to type in example.com/directory it would redirect to my 404 page. 目前,如果您要输入example.com/directory ,它将重定向到我的404页面。 You have to type in example.com/directory/index . 您必须输入example.com/directory/index My assumption was that for every directory, the default page was index with either the .php or .html extension. 我的假设是,对于每个目录,默认页面都是带有.php.html扩展名的index I guess I was wrong, so I configured that directory's .htaccess as follows: 我想我错了,所以我将目录的.htaccess配置如下:

DirectoryIndex index.php

But, no progress. 但是,没有进展。 Is there something I am missing? 我有什么想念的吗? To clarify, I would like the url of example.com/directory to open the index file in that directory. 为了明确起见,我希望example.com/directory的URL在该目录中打开索引文件。 Here is my universal .htaccess : 这是我通用的.htaccess

ErrorDocument 404 http://www.example.com/404

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

Edit 编辑

So, I commented out the 3 Rewrite commands in my htaccess and now my issue is resolved. 因此,我注释掉了htaccess中的3个Rewrite命令,现在问题已解决。 The problem is that now the php file extensions are showing. 问题是现在显示的是php文件扩展名。 Is there a way to include rewrite rules without interfering with default index pages? 有没有一种方法可以包括重写规则而不干扰默认索引页? Or is the problem somewhere else? 还是其他地方存在问题?

It looks like the 404 is due to the directory /directory.php not existing. 看来404是由于目录 /directory.php不存在。 Because your RewriteRule is executed with the condition only that it isn't a real file ( %{REQUEST_FILENAME} !-f ), the rule is mistakenly applied for directories, attempting to append .php . 因为您的RewriteRule仅在不是真实文件的情况下执行( %{REQUEST_FILENAME} !-f ),所以该规则错误地应用于目录,试图附加.php It's easily fixed with a !-d : 它很容易用!-d修复:

ErrorDocument 404 http://www.example.com/404

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]

Now, when a directory like /directory is requested, the default DirectoryIndex will be used and no rewrite will take place. 现在,当请求类似/directory ,将使用默认的DirectoryIndex并且不会进行任何重写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM