问题描述
我有一个子目录大量的文件。我想这些文件映射到根目录下,这样的http://www.mysite.com/filename的要求实际上指向http://www.mysite.com/subfolder/filename.php上服务器。我想删除引用到子文件夹。
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.
请注意,我想追加在htaccess文件的PHP后缀为好。我也有一个比较大的数字的根目录(其中约,联系等)的文件和一些其他的文件夹,以便重写基地是不是一个可行的选择。
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
感谢您的任何意见。 B计划是简单地在URL中留下的文件夹名称,但我会很高兴找到一个更好的解决方案和更短,更直接的网址。
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.
推荐答案
第二重写条件将无法工作,因为你没看里面的文件夹,尝试这样的事情:
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
如果您的文档根目录是像 / htdocs目录
然后你检查,如果该文件 /htdocs/filename.php
存在,当我想你想要的是检查 /htdocs/folder/filename.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.
这篇关于在子目录地图文件,通过根文件夹的.htaccess的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!