加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

php – htaccess – 重写具有相同名称的目录的文件

发布时间:2020-05-25 10:57:27 所属栏目:PHP 来源:互联网
导读:我有一个网站,有多个 PHP文件和目录具有相同的名称,如下所示: /projects.php/projects/projects/something.php 我已设法使用以下规则将http://example.com/projects重写为http://example.com/projects.php(使用答案here): RewriteEngine On# Disable automa

我有一个网站,有多个 PHP文件和目录具有相同的名称,如下所示:

/projects.php
/projects
/projects/something.php

我已设法使用以下规则将http://example.com/projects重写为http://example.com/projects.php(使用答案here):

RewriteEngine On

# Disable automatic directory detection
DirectorySlash Off

# Hide extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$$1.php

但是,当我明确地将目录斜杠输入到URL时,它会访问该文件夹.
马上:

http://example.com/projects   -->  http://example.com/projects.php  [file]
http://example.com/projects/  -->  http://example.com/projects/     [folder]

我知道它为什么这样做(projects / .php不是文件).我修复它的尝试包括检查它是否是一个文件夹,并用什么都替换斜线并改为访问它.

新的.htaccess:

RewriteEngine On

# Disable automatic directory detection
DirectorySlash Off

# Hide extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$$1.php

# Folder fix
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (.*)/$$1.php

这可以按预期工作,但它在客户端完全混乱,因为客户端仍然在其URL中有一个文件夹,所以当它尝试获取相对路径时,它会失败.

现在我考虑使用[R = 301]标志进行重定向,但据我所知,%{REQUEST_FILENAME}是相对于服务器的,所以重定向到它不起作用.

如果有人能指出我正确的方向,我将不胜感激!

尝试将此行放在顶部:
Options -MultiViews

Read More About it

你的.php隐藏规则应该是:

RewriteEngine On

# Disable automatic directory detection
DirectorySlash Off

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

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读