【问题标题】:htaccess: direct to php if php is present, if not, direct to directoryhtaccess:如果存在php,则直接到php,如果不存在,则直接到目录
【发布时间】:2014-10-19 16:36:38
【问题描述】:

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

如果我输入网址为 www.example.com/media 或 www.example.com/media/,我希望被定向到 www.example.com/media.php 上的文件,而不是里面的文件工作目录。 但是,如果我输入网址为 www.example.com/media/manyotherfiles,我希望被定向到 www.example.com/media/manyotherfiles.php。 (media.php 文件链接到 media 目录中的页面更直观)

如何使用我的 .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

【问题讨论】:

    标签: php apache .htaccess mod-rewrite


    【解决方案1】:

    您可以在DOCUMENT_ROOT/.htaccess 文件中使用此代码来隐藏.php 扩展:

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

    【讨论】:

    • 这不起作用 - 当我现在尝试访问任何 php 文件时出现 404 not found 错误,除了 index.php。它说找不到“/media/manyotherfiles”之类的内容。
    • 你能在浏览器中打开http://domain.com/media/manyotherfiles.php吗?确保这是规则在其他规则之前,并且它被放置在 root .htaccess
    • 是的,但我无法打开这样的 php 文件:http://domain.com/page.php。该文件处于 root 访问权限。
    • 我不明白你的意思 虽然文件处于 root 访问权限所以不清楚为什么你不能打开http://domain.com/page.php
    猜你喜欢
    • 1970-01-01
    • 2014-03-15
    • 1970-01-01
    • 2011-10-24
    • 1970-01-01
    • 1970-01-01
    • 2021-02-04
    • 2017-09-05
    • 1970-01-01
    相关资源
    最近更新 更多