关闭.htaccess中的循环,以将所有子文件夹重定向到根目录 [英] close the loop in .htaccess for redirecting all subfolders to root

查看:67
本文介绍了关闭.htaccess中的循环,以将所有子文件夹重定向到根目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用.htaccess将所有子文件夹重定向到根页面.即,我想使用以下设置将example.com/audio、example.com/video、example.com/images重定向到example.com:但是,我希望使其独立于域名.因此,您在我的.htaccess设置中看不到"example.com".

I am trying to redirect all subfolders to the root page using .htaccess. i.e. I'd like to redirect example.com/audio, example.com/video, example.com/images to example.com using below settings: However, I'd like to make it independent from the domain name. Hence, you don't see 'example.com' in my .htaccess setting.

RewriteEngine On
RewriteRule  ^([^/]+)(.*)$  / [R]

当我对其进行测试时,我的浏览器给出了重定向次数过多的错误消息"错误消息.我已经检查了apache日志,它显示请求已被重定向了很多次.我假设当htaccess将我们重定向到根目录时,.htaccess中的上述几行会再次运行,因此会导致循环.有什么方法可以强制htaccess仅重定向带有子目录的URL?

When I test it, my browser gives me "redirected you too many times error message" error message. I have checked the apache logs and it shows that the request has been redirected so many times. I assume when htaccess redirects us to the root the above lines in .htaccess get run again and therefore it makes a loop. Is there any way I can force htaccess to redirect only those URL with subdirectory ?

推荐答案

您可以尝试以下规则:

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^.+$ / [R=301]

使用此规则,除了根本身之外,所有内容都将重定向到根.

With this rule, everything will be redirected to the root except the root itself.

这篇关于关闭.htaccess中的循环,以将所有子文件夹重定向到根目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆