Marcus
Marcus

Reputation: 115

Htaccess Redirect Loop Issue

For particular reasons, I'm hosting a subsite within a parent site, and would like to use a domain alias for the subsite. For clarity, let's call the two domains parent.org and child.org.

The subsite is located at: parent.org/child

Since child.org is a domain alias, it's also located at: child.org/child

I'd like to build redirects that route any requests to either child.org or parent.org/child to child.org/child

Scouring the forums, I've attempted a number of things, though this one seemed the closest to what I'm going for:

RewriteCond %{HTTP_HOST} ^(www.)?child\.org$ [NC]
RewriteCond %{REQUEST_URI} !^/child/.*$ [NC]
RewriteRule ^(.*)$ http://child.org/child/$1 [R=302,L,NS]

RewriteCond %{HTTP_HOST} ^(www.)?parent\.org/child$ [NC]
RewriteRule ^(.*)$ http://child.org/child/$1 [R=302,L,NS]

This doesn't quite work as hoped, resulting in child.org/child/child/child/etc.

What am I missing here?

Addendum: To help clarify my goal here, for either domain, if "/child" isn't the first segment of the URL, I'd like to use parent.org. If "/child" isn't the first segment of the URL, I'd like to use child.org. So:

Parent setup:
parent.org/foo ->  parent.org/foo
parent.org/child/foo -> child.org/child/foo

Child setup:
child.org -> child.org/child
child.org/foo -> parent.org/foo
child.org/child/foo -> child.org/child/foo

Upvotes: 1

Views: 47

Answers (1)

Mohammed Elhag
Mohammed Elhag

Reputation: 4302

First RewriteCond %{HTTP_HOST} ^(www.)?parent\.org/child$ [NC] is wrong because %{HTTP_HOST} is representing either parent.org or child.org and could not match uri after them.

And i think you have no issue with child.org because it is already done before by domain alias which means that the request to child.org will show up every thing in child directory unless you do another configuration .

So , your code should looks like this , to redirect only when parent domain is involved :

RewriteCond %{HTTP_HOST} ^(www\.)?parent\.org$ [NC]
RewriteCond %{REQUEST_URI} ^/child/(.*)$ [NC]
RewriteRule ^child/(.*)$ http://child.org/$1 [R=302,L]

So , this part parent.org/child/ is already configured as child.org so any request contains parent.org/child/whatever will be child.org/whatever

Update : you siad Since child.org is a domain alias, it's also located at: child.org/child if that so ,the code should look like this :

RewriteCond %{HTTP_HOST} ^(www\.)?parent\.org$ [NC] 
RewriteCond %{REQUEST_URI} ^/child/(.*)$ [NC]
RewriteRule ^(.*)$ http://child.org/$1 [R=302,L]

RewriteCond %{HTTP_HOST} ^(www\.)?child\.org$ [NC]
RewriteCond %{REQUEST_URI} !^/child/(.*)$ [NC]
RewriteCond %{REQUEST_URI} !^/?$ 
RewriteRule ^(.*)$ http://parent.org/$1 [R=302,L]

RewriteCond %{HTTP_HOST} ^(www\.)?child\.org$ [NC]
RewriteCond %{REQUEST_URI} !^/child/(.*)$ [NC]
RewriteCond %{REQUEST_URI} ^/?$ 
RewriteRule ^(.*)$ http://child.org/child/$1 [R=302,L]

So , any request to parent.org/child/whatever will be redirected to child.org/child/whatever

Then any request to child.org/whatever and wherever is everything except child will be redirected to parent.org/whatever

Finally , any request to child.org will be redirected to child.org/child/

Upvotes: 1

Related Questions