如何使用htaccess将子文件夹中的所有文件重定向到根目录



我有一个类似www.example.com/comments/a/word1.html的目录,我从链接中删除了comments/a/部分和带有htaccess的.html。在a文件夹下,我有很多文件,比如word1.html、word2.html、word3.html,我想像这样重定向所有这些文件:www.example.com/word1www.example.com/word2www.example.com/word3

我正在使用这个。htaccess代码:

RewriteEngine on
RewriteRule ^/?comments/a/(.*)(/|.html)?$ /$1 [R=301,L] 
RequestCond %{DOCUMENT_ROOT}/comments/a%{REQUEST_URI}.html -f 
RewriteRule ^/?(.*)/? /comments/a/$1.html [END] 

但我得到了错误";找不到404页,在此服务器上找不到请求的资源&";。在这种情况下我能做什么?是否有任何方法将www.example.com/comments/a/word1.html单词2-3-4等重定向到www.example.com/word1

由于我的研究,我找到了这样的解决方案:

##Rewrite non /comments/a/ links internally start##
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/comments/a/$1.html -f
RewriteRule ^/?([^.?]+)$/?$ comments/a/$1.html [L,NC]
##Rewrite non /comments/a/ links internally end##

这个代码对我来说很有魅力。

最新更新