views:

152

answers:

1

Hi,

I would like to create virtual subdomains through htaccess in the following way.

Entering:

http://testuser.domain.com/1/2/3/

Should be processed as:

http://www.domain.com/user.php?id=testuser&var1=1&var2=2&var3=3

HOWEVER, this rewrite should not use user.php, but index.php, in case someone enters:

http://www.domain.com or http://domain.com

This is what I got so far, however it doesn't seem to work. Any help from a mod rewrite expert would be much appreciated. Thanks in advance.

Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www\.)?domain.com$ [NC]
RewriteRule ^(.*)/?(.*)/?(.*)/?$ index.php?var1=$1&var2=$2&var3=$3 [NC,QSA,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !^(www|mail).domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+).domain.com$ [NC] 
RewriteRule ^(.*)/?(.*)/?(.*)/?$ user.php?id=%2&var1=$1&var2=$2&var3=$3 [NC,QSA,L]
A: 

Your rules are actually very close to doing what you want them to. The only problem that I can see is in your test patterns for your two RewriteRule statements. Currently, you have

RewriteRule ^(.*)/?(.*)/?(.*)/?$ ...

...which happens to be equivalent to this:

RewriteRule ^(.*)$

This is because everything past the first capture group can match nothing and still be considered a match, so that first group is greedy and matches the whole input string without needing to defer to the other parts of the pattern.

Since the capture groups shouldn't capture forward slashes anyway, as they're being used as a variable delimiter here, the straightforward fix is to change them to [^/]*, as so:

Edit: I also modified the RewriteCond set in the second group to ignore the !-f condition in the case of /index.php, which will happen if you request the subdomain without anything after the domain.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www\.)?domain.com$ [NC]
RewriteRule ^([^/]*)/?([^/]*)/?([^/]*)/?$ index.php?var1=$1&var2=$2&var3=$3 [NC,QSA,L]

RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_URI} ^/index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !^(www|mail).domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+).domain.com$ [NC] 
RewriteRule ^([^/]*)/?([^/]*)/?([^/]*)/?$ user.php?id=%2&var1=$1&var2=$2&var3=$3 [NC,QSA,L]
Tim Stone
Hi Tim,Thanks for your quick reply.I altered the htaccess file, but it seems the problem might be situated elsewhere. It seems that the user.php file is only displayed when a variable is entered along with it.To give an example:http://www.domain.com/ --> (shows index.php)http://domain.com/1/2/3/ --> (shows index.php)http://testuser.domain.com/1/2/3/ --> (shows user.php) So far so good, BUT: http://testuser.domain.com/ --> (shows index.php) Do you have an idea how I can make it so that even without any extra variables, it will always show user.php for a subdomain?Best Regards
Yah
Oh, whoops, the one scenario I didn't test. I'll make an edit that should take care of that.
Tim Stone
Hi Tim, I adjusted it again, but it still doesn't seem to work. Thanks to your last edit http://testuser.domain.com/index.php is now showing the user.php file correctly. However, just like before http://testuser.domain.com/ still displays the index.php file instead of the user.php file.
Yah
@Yah - Hmm, it works fine on my test server. Do you have full access to the server's configuration, or no?
Tim Stone
Hi Tim, No, not entirely. Which settings do you think I should check/adjust? Best Regards
Yah
@Yah - Well, I was going to recommend using [`RewriteLog`](http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritelog) to check what was matching and what was, but you have to define it in your virtual server configuration. I'm trying to think of an alternate way to see how the process in your environment differs from mine, as I'm sure it's something easy to fix...once we find the problem, anyway.
Tim Stone
Yah
Yeah, one of the conditions must be failing for some reason. What happens if you remove `RewriteCond %{REQUEST_FILENAME} !-d`?
Tim Stone
Weird, but that actually solved it. :) Thanks a lot Tim. Best Regards
Yah
Good stuff, glad it's finally working!
Tim Stone