【问题标题】:Remove a folder from url when folder still exists with .htacces当 .htaccess 文件夹仍然存在时,从 url 中删除文件夹
【发布时间】:2016-02-09 16:50:06
【问题描述】:

我从一个网站开始。我希望我所有的 php 文件都在一个名为 files 的文件夹中,所以这些文件都不会在根目录中。

我不想在 url 上看到文件夹 files,因此当我访问其中一个文件时,它应该显示 www.mywebsite.com/thefile.php 而不是 www.mywebsite.com/files/thefile.php

这是我的 .htaccess 中的内容(仅更改了索引文件)。

<IfModule mod_rewrite.c>
    DirectoryIndex files/home.php
</IfModule>

一些额外的可能很重要的信息。这是一个测试网站,我已将其放在另一个使用 Magento 制作的工作网站的特定文件夹中。

【问题讨论】:

  • 为什么不将您的DocumentRoot 设置为files/ 目录?
  • 我在实际根目录中的其他文件夹是否仍然有效?我的意思是,我还有一些其他文件夹,例如 imgs、css 和 js 用于此类文件。
  • 所以您只想要.php 中的/files/ 文件?
  • 如果可能的话,是的。如果没有,将其余文件夹放在 /files/ 中也是一种选择

标签: .htaccess mod-rewrite path directory


【解决方案1】:

您可以在testing/.htaccess 文件中使用此代码:

RewriteEngine On
RewriteBase /testing/

RewriteCond %{THE_REQUEST} /files/([^?]+?\.php)[\s?] [NC]
RewriteRule ^ %1 [R=302,NE,L]

RewriteRule ^([^/.]+\.php)$ files/$1 [NC,L]

【讨论】:

  • 这不起作用。我把这个网站放到另一个文件夹中,比如:www.mywebsite.com/testing,我有我的.htaccess,然后是文件夹files
  • 有 2 个文档根。一个用于工作网站(在www 内),另一个在www/testing/ 上,其中有文件夹文件(www/testing/files/
  • 当我尝试访问我的任何文件时,它给了我错误:内部服务器错误
  • 成功了!非常感谢您的时间和耐心:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-06
  • 2015-06-23
  • 2015-07-27
  • 1970-01-01
  • 1970-01-01
  • 2022-09-27
  • 1970-01-01
相关资源
最近更新 更多