问题
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