Forum Moderators: phranque

Message Too Old, No Replies

Setting up a dynamic subdomain

What needs to be done on the host's end to make it work?

         

nfs2

6:30 pm on May 6, 2006 (gmt 0)

10+ Year Member



I know there are a lot of posts here on how to use mod_rewrite to create dynamic subdomains for user based sites. Like user.example.com instead of example.com/user

My question is, what has to be enabled on the hosts side to make it work? I tried all the codes, and none of them work, probably because something on the hosts side isnt set up for this.

I have cPanel, with a subdomain moduel. Do i simply try to make a * subdomain? Is there something specific i need to ask my host to do?

Thanks in advance

nfs2

8:39 pm on May 6, 2006 (gmt 0)

10+ Year Member



OK i asked my host and they said they set it up, whatever that means.. But if i enter anything.mysite.com it takes me to my home page, but if i simply enter mysite.com (no subdomain) it takes me to the cPanel notice "no website is configured at this address".

Basically my users have a blog at mysite.com/journal?user=name, and i want them to access it by going to name.mysite.com

heres the code im using, but no matter what subdomain i enter, it just takes me to the home page

RewriteCond %{HTTP_HOST}!^(www\.)?mysite\.com [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.mysite\.com [NC]
RewriteRule .* /journal?user=%1 [R=301,L]

Can anyone help?

nfs2

8:58 pm on May 6, 2006 (gmt 0)

10+ Year Member



Im searching and trying different codes.. This one gives me a 500 error whenever the subdomain is NOT www

RewriteCond %{REQUEST_URI}!^/journal?user=
RewriteCond %{HTTP_HOST}!^www\.(mysite\.com)
RewriteCond %{HTTP_HOST} ^([a-z][a-z0-9_-]+[a-z0-9])\.(mysite\.com)
RewriteRule (.*) /journal?user=%1/$1 [L]

it worked for smoeone else..

jdMorgan

5:51 am on May 7, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> Is there something specific i need to ask my host to do?

Yes, ask them to add ServerAliases of "<yourdomain>.com" and "*.<yourdomain>.com" to your VirtualHost container. They may be happy to do it, or refuse -- no way to know ahead of time. The first alias will fix your problem with the www-less domain, and you also need "wildcard subdomains" defined at both the DNS and virtual server level.

You basically want to point all subdomains to the *same* place in the filesystem that your www subdomain now points to. Then you can use your mod_rewrite code to move the subdomain name into a query parameter and call your script. This generally cannot be done using a "Control Panel" because they are almost always set up to point each subdomain to a unique subdirectory.

The most common cause of 500-Server Errors when copying code from this forum is that the forum posting cleanup code removes spaces preceding "!" characters. For example, the code you posted in msg#2 should be:


RewriteCond %{HTTP_HOST} !^(www\.)?mysite\.com [NC]
RewriteCond %{HTTP_HOST} ^([^\]+)\.mysite\.com [NC]
RewriteRule .* /journal?user=%1 [R=301,L]

with a space before the "!" in the first line.

The second code snippet does not look like it would work to me; query strings are not available in the server REQUEST_URI variable.

Jim

nfs2

6:46 am on May 7, 2006 (gmt 0)

10+ Year Member



Thanks for the reply. My webhost set up a wildcard subdomain, and the fix you made for the first code worked. The thing is i need name.mysite.com to be the url people see in their address bar, but the code just redirects them to the long url. Is there a change i can make to the code to fix that?

nfs2

5:20 pm on May 8, 2006 (gmt 0)

10+ Year Member



My webhost fixed the issue with the www, but im still hvaing trouble trying to figure out how to keep the url name.mysite.com, rather then simply redirecting..

Its kinda hard to figure out cause i dont even know what search terms would be helpful for this

jdMorgan

3:04 am on May 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just change the rule to:

RewriteRule .* /journal?user=%1 [L]

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

nfs2

3:57 am on May 9, 2006 (gmt 0)

10+ Year Member



I thought it might be that simple, so its what i tried first. Unfortuantly it gives me a 500 error.

I dont know if something else might be interfering, so heres my whole htaccess file..

RewriteEngine on

RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

<FilesMatch "^[^\.]+$">
ForceType application/x-httpd-php
</FilesMatch>

RewriteCond %{HTTP_HOST}!^(www\.)?example\.com [NC]
RewriteCond %{HTTP_HOST} ^([^\]+)\.example\.com [NC]
RewriteRule .* /journal?user=%1 [L]

DirectoryIndex index.html.var index.htm index.html index.php index
php_value register_globals Off
php_value track_vars On
php_value arg_separator.output "&amp;"
php_value arg_separator.input "&"

nfs2

6:17 pm on May 9, 2006 (gmt 0)

10+ Year Member



I got it! The following code works..

RewriteCond %{HTTP_HOST}!^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\. [NC]
RewriteRule ^$ /journal?user=%1 [L]

Now i just have to figure out the rest of the urls, for example

changing www.example.com/journal?user=myname&view=pictures

to

myname.example.com/pictures

I'll try to figure it out on my own, but if anyone can come up with a solution before me, i'll appreciate it :)

nfs2

7:21 pm on May 9, 2006 (gmt 0)

10+ Year Member



Ok i figured that out too, the following code works

RewriteCond %{HTTP_HOST}!^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\. [NC]
RewriteRule ^pictures$ /journal?user=%1&view=pictures [L]

The problem now is when i try to view an individual picture. The url is normally www.mysite.com/journal?user=myname&view=pictures&pic=file.jpg

So i thought the following code would work, but it doesnt.

RewriteCond %{HTTP_HOST}!^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\. [NC]
RewriteRule ^pictures/pic/([a-z_0-9]+)/$ /journal?user=%1&view=pictures&pic=$1 [L]

So the url myname.mysite.com/pictures/pic/file.jpg should work, but i get the error associated with the username not being registered, which is confusing to me.. because myname.mysite.com works, and myname.mysite.com/pictures works..

Im stumped on this one.. Anyone see what im doing wrong?

jdMorgan

1:16 pm on May 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You'll need to use the [QSA] flag if you wish to append a new variable to the existing query string. Othewise, the default behaviour is to *replace* the existing query.

Also, if you wish to use a pattern in the rule that can match the destination URL, you will need to explicitly exclude that URL to avoid rewrite 'looping':


# Add one of the two following RewriteConds to prevent
# looping, whichever seems more appropriate
RewriteCond %{REQUEST_URI} !^/journal
RewriteCond %{QUERY_STRING} !user=.
#
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\. [NC]
RewriteRule .* /journal?user=%1 [b][QSA,[/b]L]

Jim