Forum Moderators: phranque

Message Too Old, No Replies

Using mod_rewrite to create virtual subdomains

can't seem to get it to work correctly

         

roldar

8:13 am on Aug 4, 2004 (gmt 0)

10+ Year Member



I've got a site where people register a username like "trekkiefan", and it gives them an entry in my database. Then they can go to www.domain.com/mypage.php?id=trekkiefan it brings up their customized page.

I want to allow these people to just type in myusername.domain.com and have it bring up the page www.mydomain.com/mypage.php?id=myusername, but I want the address bar to remain at myusername.domain.com.

I've got the following .htaccess file, but every time I try to enter myusername.domain.com it doesn't work at all.

RewriteEngine On
# Extract the subdomain part
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC]
# Verify it's not ``www''
RewriteCond %1!^www$ [NC]
# Make a proxy redirection
RewriteRule ^.*$ [domain.com...] [P,L]

Does anybody have a suggestion?

roldar

8:23 am on Aug 4, 2004 (gmt 0)

10+ Year Member



I tried the following variation as well, but it didn't work either:

Options +FollowSymlinks
RewriteEngine On
# Extract the subdomain part
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC]
# Verify it was not www
RewriteCond %1!^www$ [NC]
# Do internal redirection for a request as
RewriteRule ^$ /mypage.php?id=%1

jdMorgan

4:14 pm on Aug 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You may have run into one of mod_rewrite's traps: Back-references from RewriteRule to RewriteCond only apply to the last-matched RewriteCond. So, you need to re-arrange a little:

Options +FollowSymlinks
RewriteEngine On
# Verify subdomain is not www
RewriteCond %{HTTP_HOST} !^www [NC]
# Extract the subdomain part
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com [NC]
# Do internal rewrite from subdomain/ to script with id
RewriteRule ^$ /mypage.php?id=%1 [L]

As with your original code, this only applies to the root directory, so the id and page-handling will have to be maintained by your script.

Jim

roldar

4:45 am on Aug 5, 2004 (gmt 0)

10+ Year Member



I made the changes but for some reason they don't seem to be doing anything. I was having problems with an old .htaccess configuration so I went through my directories and deleted every instance of it I could find, replacing them with the new one. Even after deleting them, some of the old redirect rules I created in the old .htaccess file are in effect. Is there some place in particular that a .htaccess file could be where it would remain in effect? I've replaced the ones in all the following directories: /, /var/, /var/www/, /var/www/html/, and /var/www/html/mydomain/ and yet the old rules are still causing problems. Maybe if I could find the place where these rules are coming from I could put the new .htaccess there and it would allow for my virtual subdomain configuration.

The other possibility I've considered is that mod_rewrite isn't turned on. Is there some place where I can find out if mod_rewrite is on?

Does anybody else do this "virtual subdomain" creation with .htaccess? If so, would you mind sticky-mailing me the name of your webhost so I can take a look at their plans? I've run into more than a couple problems with my current host and I think I should find a good one before my site gets much activity.

jdMorgan

5:51 am on Aug 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The only places you would normally have an .htaccess file are in /var/www/html/mydomain/ and subdirectories below that (but only if needed to create specific rules for those subdirectories only).

The simplest way to find out if mod_rewrite is enabled is to make a test rule:


Options +FollowSymLinks
RewriteEngine on
RewriteRule ^silly\.html$ /index.html [L]

where "silly.html" is a file that does not exist, and "index.html" is a file that does exist. Put this code in .htaccess in your Web root directory (wherever your "home page" is). Then use your browser to request "silly.html" from your site. If mod_rewrite is active, the server will give you the contents of "index.html" in response.

In order for this subdomain plan to work, you must have wild-card DNS set up to point all subdomains to your server, and your host must point all subdomain requests to your Web root directory where the .htaccess code is installed.

Jim