htaccess将子文件夹(第二级)中的文件重写到文件夹(第一级) [英] htaccess to rewrite file in subfolder (2nd level) to folder (1st level)
问题描述
当前,我正在使用下面的htaccess代码加载 http://www.domain. com/folder/file.php 来自 http://www.domain.com/file ,从URL中删除文件夹名称"/folder/"和文件扩展名".php".
Currently, I'm using the below htaccess code to load http://www.domain.com/folder/file.php from http://www.domain.com/file, removing the folder name "/folder/", and the file extension ".php", from the URL.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/folder%{REQUEST_URI}\.php -f [OR]
RewriteCond %{DOCUMENT_ROOT}/folder%{REQUEST_URI} -d
RewriteRule ^(.*)$ /folder/$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
现在,我正在尝试执行几乎相同的操作,但是文件存储在子文件夹中.
Now, I'm trying to do almost the same, but with a file that is stored in a subfolder.
因此,换句话说,我要呼叫 http://www.domain. com/folder/subfolder/file.php 来自 http://www.domain.com/subfolder /file
So, in other words, I want to call http://www.domain.com/folder/subfolder/file.php from http://www.domain.com/subfolder/file
在这种情况下,隐藏扩展文件不是问题,因为上面代码中的最后2行在全局范围内起作用,问题在于重写(Rewriting)问题,仅通过添加子文件夹"名称是行不通的.我以为这会起作用,但事实并非如此:
In this case, hiding the extension file is not a problem as the 2 last lines from the code above works globally, the issue is with the Rewriting which doesn't work simply by adding the 'subfolder' name. I thought this was going to work, but it wasn't:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/folder/subfolder%{REQUEST_URI}\.php -f [OR]
RewriteCond %{DOCUMENT_ROOT}/folder/subfolder%{REQUEST_URI} -d
RewriteRule ^(.*)$ /folder/subfolder/$1.php [L]
感谢您的帮助.
推荐答案
用这两个规则替换您的两个规则:
Replace both of your rules with this:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/folder/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /folder/$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
这篇关于htaccess将子文件夹(第二级)中的文件重写到文件夹(第一级)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!