首页 > 解决方案 > Redirect loop while looking for uri that does NOT contain specific string

问题描述

I have an site lets say https://example.com/ and I would like to redirect every url that doesn't begins with /something to https://example.com/something/

I'm using Apache 2.4.29 (hosting) and my .htaccess looks like this.

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/something(.*)$ [NC]
RewriteRule ^(.*)$ /something/ [R=302,NC,L]

My problem is that when I'm on homepage (/) or any other page I'm redirected to /something/ which is correct but when I'm on /something/ I'm still beeing redirected to /something/ and it loops until ERR_TOO_MANY_REDIRECTS error shows up.

Here is a link to htaccess tester which shows that this should work and should not redirect me to /something/ when I'm already here but it is not the case on my hosting.

I was following this and this question but without success.

标签: .htaccessredirectmod-rewriteurl-rewriting

解决方案


With your shown attempts, please try following set of rules. Please place them on top of htaccess rules file in case you already have existing rules.

Please make sure to clear your browser cache before testing your URLs.

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/?$
RewriteCond %{THE_REQUEST} !something [NC]
RewriteRule ^ /something? [R=302,NC,L]

推荐阅读