如何使用 htaccess 将子文件夹中的所有文件重定向到根目录
我有一个像 www.example.com/comments/a/word1.html
的目录,我删除了 comments/a/
部分和 .html
,从链接访问.htaccess。在 a
文件夹下,我有很多文件,例如 word1.html、word2.html、word3.html,我想像这样重定向所有这些文件: www.example.com/word1
www.example.com/word2
www.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
word2-3-4 等也重定向到 www.example.com/word1
i have a directory like www.example.com/comments/a/word1.html
I removed comments/a/
section and .html
with htaccess from link. Under the a
folder i have lots of files like word1.html, word2.html , word3.html, and i want to redirect all of these files like this:www.example.com/word1
www.example.com/word2
www.example.com/word3
I am using this .htaccess code:
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]
but i got the error "404 page not found, The resource requested could not be found on this server!". What can i do in this situation? Is there any way to redirect www.example.com/comments/a/word1.html
word2-3-4 etc. also to www.example.com/word1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过研究,我找到了这样的解决方案:
这段代码对我来说就像一个魅力。
As a result of my researchs i found a solution like this :
This code works like a charm for me.