通过.htaccess将子目录中的文件映射到根文件夹

发布于 2024-12-12 01:39:16 字数 658 浏览 0 评论 0原文

我的子目录中有大量文件。我想将这些文件映射到根目录,以便“http://www.mysite.com/filename”的请求实际上指向“http://www.mysite.com/subfolder/filename.php”服务器。我想删除对子文件夹的引用。

请注意,我还想在 htaccess 文件中附加“php”后缀。我的根目录中还有相对大量的文件(包括“关于”、“联系人”等)和其他几个文件夹,因此“重写基础”不是一个可行的选择。

理想情况下,对于每个请求,我会测试文件是否存在于根文件夹中,如果不存在,则映射到子文件夹。我尝试了以下操作,但无法将文件映射到子文件夹,更不用说先检查它是否存在。

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
# works fine if not commented:
# RewriteRule ^(.*)$ $1.php
# try pointing to subfolder instead does not work
RewriteRule ^(.*)$ folder/$1.php

感谢您的任何建议。 B 计划是简单地将文件夹名称保留在 URL 中,但我很高兴能找到更优雅的解决方案和更短、更直接的 URL。

I have a large number of files in a sub directory. I would like to map these files to the root directory such that the request for "http://www.mysite.com/filename" actually points to "http://www.mysite.com/subfolder/filename.php" on the server. I would like to remove the reference to the sub-folder.

Note that I'd like to append the "php" suffix in the htaccess file as well. I also have a relatively large number of files in the root directory (including About, Contact, etc.) and a couple other folders so Rewrite Base is not a viable option.

Ideally, for each request, I would to test whether or not the file exists in the root folder, and if not, then map to the subfolder. I tried the following but could not map a file to a subfolder, let alone check if it exists first.

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
# works fine if not commented:
# RewriteRule ^(.*)$ $1.php
# try pointing to subfolder instead does not work
RewriteRule ^(.*)$ folder/$1.php

Thanks for any advice. Plan B is to simply leave the folder name in the URL but I would be thrilled to find a more elegant solution and a shorter, more direct URL.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赤濁 2024-12-19 01:39:16

第二个重写条件将不起作用,因为您没有查看“文件夹”内部,请尝试如下操作:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/folder%{REQUEST_URI}.php -f
RewriteRule ^(.*)$ folder/$1.php

如果您的文档根目录类似于 /htdocs 那么您正在检查文件是否 /htdocs当我认为您想要检查 /htdocs/folder/filename.php 是否存在时, /filename.php 就存在。

The 2nd rewrite condition will not work because you are not looking inside "folder", try something like this:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/folder%{REQUEST_URI}.php -f
RewriteRule ^(.*)$ folder/$1.php

If your document root was something like /htdocs then you were checking if the file /htdocs/filename.php exists when I think what you want is to check if /htdocs/folder/filename.php exists.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文