Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache .htaccess检查文件是否存在于多个子文件夹中并重写_Apache_.htaccess_Mod Rewrite_Url Rewriting - Fatal编程技术网

Apache .htaccess检查文件是否存在于多个子文件夹中并重写

Apache .htaccess检查文件是否存在于多个子文件夹中并重写,apache,.htaccess,mod-rewrite,url-rewriting,Apache,.htaccess,Mod Rewrite,Url Rewriting,假设我在Apache服务器上有一个静态网站,其文件夹结构如下: http://www.example.com | +-- /news/ | | | +-- page1.html | +-- page2.html | +-- ... | +-- /otherstuff/ /news/文件夹中页面的url为: http://www.example.com/news/page1.html http://www.example.com/news/page2.html 现在,/

假设我在Apache服务器上有一个静态网站,其文件夹结构如下:

http://www.example.com
|    
+-- /news/
|  |  
|  +-- page1.html
|  +-- page2.html
|  +-- ...
|
+-- /otherstuff/
/news/文件夹中页面的url为:

http://www.example.com/news/page1.html  
http://www.example.com/news/page2.html
现在,/news/文件夹中的文件数量正在增加,我想在/news/中创建新的子文件夹,并在这些新目录中拆分文件,但我还想在URL中隐藏子文件夹的名称

新文件夹结构将是:

http://www.example.com
|    
+-- /news/
|  |  
|  +-- /subfolder1/
|  |   |
|  |   +-- page1.html
|  |
|  +-- /subfolder2/
|  |   |
|  |   +-- page2.html
|  +-- ...
|
+-- /otherstuff/
但是页面的URL必须保持不变:

http://www.example.com/news/page1.html  
http://www.example.com/news/page2.html
http://www.example.com/news/subfolder1/page1.html  
http://www.example.com/news/subfolder2/page2.html
而不是:

http://www.example.com/news/page1.html  
http://www.example.com/news/page2.html
http://www.example.com/news/subfolder1/page1.html  
http://www.example.com/news/subfolder2/page2.html
有没有一种方法可以使用.htaccess中的重写规则获得此结果

我读过这个问题:

但被接受的答案对我来说并不适用


提前感谢您的建议。

您可以在/news/.htaccess中使用以下规则:

RewriteEngine on

RewriteCond %{DOCUMENT_ROOT}/subfolder/$1.html -f
RewriteRule ^(.*?)\.html$ /news/subfolder/$1 [L]
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?:news/)?(.*?)\.html$ /news/subfolder/$1.html [L] 
如果/news/subfolder/中存在file.html,则将/news/file.html重写为/news/subfolder/file.html

如果您的htaccess位于root中,则可以尝试以下操作

RewriteEngine on

RewriteCond %{DOCUMENT_ROOT}/news/subfolder/$1.html -f
RewriteRule ^news/(.*?)\.html$ /news/subfolder/$1.html [L]
如果上述示例失败,您可以在root或news/.htaccess中尝试此操作:

RewriteEngine on

RewriteCond %{DOCUMENT_ROOT}/subfolder/$1.html -f
RewriteRule ^(.*?)\.html$ /news/subfolder/$1 [L]
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?:news/)?(.*?)\.html$ /news/subfolder/$1.html [L] 

嗨,谢谢你的帮助。我尝试了你的代码,但仍然没有成功:url/news/file.html给出了404,即使/news/subfolder/file.html中的文件存在。我开始认为服务器变量有问题,因为如果我转到/news/subfolder/news1.html,代码%{DOCUMENT_ROOT}/news/subfolder/$1.html变成C:/xampp/htdocs/news/subfolder/news/subfolder/subfolder/file.html,这是某些服务器上的问题。我会为你发布一个有效的解决方案。你做到了!现在它工作了,非常感谢!!还有一个小问题:是否可以将/news/subfolder/xxx.html重定向到/news/xxx.html?