Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

UNSOLVED - Dynamic redirect based on domain, hide parent node from URL


Go to End


3 Posts   1775 Views

Avatar
DesignCollective

Community Member, 66 Posts

3 March 2011 at 1:45pm

Edited: 03/03/2011 1:49pm

Hey everyone!

It's either I am an idiot or I'm not seeing something very obvious. (probably the former :)

I am trying to do a redirect based on the domain that the request is coming. I have several domains parked. In my SiteTree I have a simple structure - a holder page on the top level, one for each domain, named after the domain without the .com/.net part.

Like this:

> Root
->Domain1
-->SubpageA1
-->SubpageA2
-->...
->Domain2
-->SubpageB1
-->SubpageB2
...

When a request to my default domain, the regular deep linking is applied, eg.

www.defaultdomain.com/domain1/subpageA1 is alright.

When a request comes from Domain1.com, however, I would like the url to be parsed without repeating domain, eg:

www.domain1.com/subpageA1

instead of

www.domain1.com/domain1/subpageA1

In .htaccess, I have tried:

RewriteCond %{HTTP_HOST} !www\.defaultdomain\.com [NC]
RewriteCond %{HTTP_HOST} ^www\.([A-Z,a-z,0-9,\_]*).* [NC]
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* sapphire/main.php?url=%1/%2&%{QUERY_STRING} [L]

but it's not working. Then I notice the url variable in htaccess, which I assume gets processed somewhere - at which point?
Perhaps I should pass the whole thing, domain, and deep links, to the url and then work with it somewhere in SilverStripe? e.g. htaccess would send a full request to sapphire/main.php?url=www.domain1.com/subpageA1, then I would somehow split the url variable in PHP and then display the correct page. No clue if there's a different method than Director::redirect to pull a particular Page from the SiteTree and display it without changing the actual URL.

I -have- tried Director::redirect... but that one displays the new url with the redirect. I was even able to get the appropriate page ID based on the domain, but I can't seem to get Director to just display a particular page object, but not redirect.

Hope this makes sense, I've been killing myself over this for 2+ months.

Avatar
Devlin

Community Member, 344 Posts

3 March 2011 at 9:48pm

Edited: 03/03/2011 9:57pm

You do know that there is a field in the behaviour tab called "Use this page as the 'home page' for the following domains"?

Avatar
DesignCollective

Community Member, 66 Posts

4 March 2011 at 11:18am

Edited: 05/03/2011 6:40am

Ha, in two years working with Silverstripe I never realized it did just what I needed :) Thanks a ton!

EDIT:

Actually, the function is not working as I would like it to. If I remove all secondary domain specific htaccess rules, the problem arises with sublinking.

E.g. I want to display http://secondarydomain.com/home. which in the sitetree is actually (root)/secondarydomain/home. Using the previous link, it will show the root (root)/home. I don't want the parent page to be visible in my url for all secondary domains. To put it another way, I need a 1st-level url link to display a 2nd level page.