Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
htaccess:如果存在php,则直接指向php;如果不存在,则直接指向目录_Php_Apache_.htaccess_Mod Rewrite - Fatal编程技术网

htaccess:如果存在php,则直接指向php;如果不存在,则直接指向目录

htaccess:如果存在php,则直接指向php;如果不存在,则直接指向目录,php,apache,.htaccess,mod-rewrite,Php,Apache,.htaccess,Mod Rewrite,我在www.example.com/media.php上有一个php文件 我在www.example.com/media/manyotherfiles.php上也有一个同名目录 如果我以www.example.com/media或www.example.com/media/的形式输入url,我希望指向www.example.com/media.php上的文件,而不是工作目录中的文件。 但是,如果我以www.example.com/media/manyotherfiles的形式输入我的url,我希

我在www.example.com/media.php上有一个php文件 我在www.example.com/media/manyotherfiles.php上也有一个同名目录

如果我以www.example.com/media或www.example.com/media/的形式输入url,我希望指向www.example.com/media.php上的文件,而不是工作目录中的文件。 但是,如果我以www.example.com/media/manyotherfiles的形式输入我的url,我希望指向www.example.com/media/manyotherfiles.php。更直观的是,media.php文件链接到媒体目录中的页面

如何使用.htaccess文件执行此操作?到目前为止,我已经删除了.php文件扩展名:

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

Options -Indexes
此外,我目前正在本地服务器上的beta目录上工作,因此,例如,我的文件如下:

ip.address.here/beta/index.php
ip.address.here/beta/media.php
ip.address.here/beta/media/manyotherfiles.php

您可以在文档\u ROOT/.htaccess文件中使用此代码来隐藏.php扩展名:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]

这不起作用-当我现在尝试访问任何php文件时,除了index.php之外,我得到一个404NotFound错误。上面说找不到像/media/manyother这样的文件。你能打开吗http://domain.com/media/manyotherfiles.php 在浏览器中?确保这是一条规则,位于其他规则之前,并且它位于root.htaccessYes中,但我无法像这样打开php文件:http://domain.com/page.php. 但是文件在根目录下访问。我不明白你说的文件在根目录下访问是什么意思,所以不清楚你为什么不能打开http://domain.com/page.php