Forum Moderators: phranque

Message Too Old, No Replies

Dynamic subdomains, and force www otherwise?

         

CodilX

12:21 pm on Nov 23, 2011 (gmt 0)

10+ Year Member



Hello,

I'm currently offering my clients on my web to have their own mini contact websites with a dynamic subdomain. The urls currently look like:

http://user.mydomain.com/website/


I have to use web/ to show .htaccess what I need, so it looks like this:

RewriteCond %{HTTP_HOST} ^(.*).mydomain.com$
RewriteCond %{REQUEST_URI} ^/website/.*
RewriteRule (.*) /client_websites/index.php [L]


What I want is to remove the /website/ prefix altogether, so that the client websites would have regular subdomains like:

http://user.mydomain.com/


And in addition, I want to force the www. prefix if the user is browsing the main website. So if the prefix is www. - display the website as normal, if there is no www. or subdomain - redirect to the same url only with www in front, and if not www. - use the client_websites/index.php to display the user content.

So what I need in .htaccess is to create a rule that checks if the HTTP_HOST (.*).mydomain.com is NOT www, and then use the same /client_websites/index.php, and then I'd add an additional check to make sure the subdomain exists.

Is this at all possible with mod_rewrite? I could force every page to go through a .php file to check if the subdomain is www or not, but that would obviously increase the server load dramatically.

Any tips would be helpful :) thanks!

CodilX

1:20 pm on Nov 23, 2011 (gmt 0)

10+ Year Member



Well I managed to do this with a combination of tutorials :)

Here's what I came up with:

RewriteCond %{HTTP_HOST} !^www\.mydomain.com$ [NC]
RewriteCond %{HTTP_HOST} ([^.]+)\.mydomain.com
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ client_websites/index.php [L]

RewriteCond %{HTTP_HOST} ^mydomain.com [NC]
RewriteRule (.*) [mydomain.com...] [R=301,L]

phranque

2:27 am on Nov 24, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



what is going to prevent requests for non-canonical domain from being resolved?

when absolutelymeaninglessgarbage.example.com is rewritten to client_websites/index.php is it going to get a 404 response?

lucy24

11:37 am on Nov 24, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Why do you need the !-f line if the preceding two Conditions are meant to ensure that you only get requests for nonexistent locations? That is, the user may think there's a foobar.mydomain.com, but it's all done with smoke, mirrors and php.

Oh, and if you're not doing anything with the request ^(.*)$ you don't need to capture it. But I should think you do want to do something with it, like feed it to the php in the form of a query string.