close the loop in .htaccess for redirecting all subfolders to root

雨燕双飞 提交于 2019-12-12 01:44:29

问题


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]

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 ?


回答1:


You can try this rule:

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

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



来源:https://stackoverflow.com/questions/38452583/close-the-loop-in-htaccess-for-redirecting-all-subfolders-to-root

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!