Teej
Teej

Reputation: 12873

RewriteRule results in infinite redirection loop

The title doesn't give justice to the question.

I have a site and I want it to be redirected to a subfolder.

I have this right now:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.net$ [OR]
RewriteCond %{HTTP_HOST} ^www.mysite.net$
RewriteRule ^(.*)$ "http\:\/\/www\.mysite\.net\/subfolder$1" [R=301,L]

The problem is that it results in a infinite redirect loop. It gets to / and redirects it to a subfolder and when it arrives there, it again redirects it.

How do I stop the redirection as soon as it gets to my designated subfolder.

Upvotes: 1

Views: 3131

Answers (3)

Manu
Manu

Reputation: 21

Also, for the RewriteCond, you can do this:

RewriteCond %{HTTP_HOST} ^(www.)?mysite.net$

to save a line

Upvotes: 0

Teej
Teej

Reputation: 12873

I think I got it right now:

RewriteEngine on
RewriteRule !^(subfolder/) http://www.mysite.net/subfolder/ [L,R]

Got a little inspiration from a deleted answer here:

Upvotes: 1

Lachlan Roche
Lachlan Roche

Reputation: 25946

Add an exclusion rule for the target.

RewriteEngine on

RewriteRule ^/subfolder - [L]

RewriteCond %{HTTP_HOST} ^mysite.net$ [OR]
RewriteCond %{HTTP_HOST} ^www.mysite.net$
RewriteRule ^(.*)$ "http\:\/\/www\.mysite\.net\/subfolder$1" [R=301,L]

Upvotes: 0

Related Questions