views:

463

answers:

4

Hello,

I'm a little stuck in here. I need to get some help with this subdomain-situation.

I need to redirect http://dynamicsubdomain.example.com/ to http://dynamicsubdomain.example.com/account/welcome.html. How do I do this? I tried several things but all without result. The main problem is that I can't fetch the entered dynamic subdomain from the %{HTTP_POST} string from mod_rewrite.

Another issue would be that it's creating and endless loop. So it only needs to redirect on these conditions, not when there's a URL like http://dynamicsubdomain.example.com/test/page.html. Because else it will create and endless loop. It's just for the starting page from the website.

I hope y'all can help me out, it's one of the last but important things from my project.

Thanks in advance!

+1  A: 

There are several options on the URL redirection wiki page. For example, how about dropping an index.php in the root that redirects to the destination?

header("Location: http://dynamicsubdomain.example.com/account/welcome.html");
Scott W
+1  A: 

Why does the domain matter so much if you are staying on the same domain, and just redirecting to a different path?

The UseCanonical setting in Apache may have an effect on this, but it is defaulted to on, which preserves the host and port specified in the request.

RewriteRule ^/$ /account/welcome.html [R,L]
gahooa
A: 

Hey guys, thanks for your support/help but I found the solution myself. Quicker than I thought :)

This is what does the trick, I hope I can help someone with this:

RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteCond %{HTTP_HOST} ^([A-Za-z0-9]+).example\.com [NC]
RewriteRule ^ http://%1.example.com/account/welcome.html [L]

@gahooa: I need to do this because my mainpage example.com is just a sort of landing-page with no special things. The extra part of the URL "account/welcome.html" is for showing a page related to the "subdomains"-account (gahooa.example.com for example will show your profile page). In my app I catch up the subdomain by a preg_match so it knows witch account it has to load. I hope I'm clear :) But thanks anyway!

This is my first time i'm using Stackoverflow but it actually works great! Quick replies from experts, great work! I definitely will come back :)

Christiaan
A: 

If you really want to use HTTP_HOST:

RewriteRule ^$ http://%{HTTP_HOST}/account/welcome.html [L,R]

But like gahooa already said you don’t specify an absolute URL if you stay with the same host. The URL path will suffice:

RewriteRule ^$ /account/welcome.html [L,R]
Gumbo