Question

I try to make subdomains with htaccess. it's working fine so far. I'm using the subdomain as a Get-Parameter within another script.

However, if someone just uses this parameter, the subdomain-parameter is overwritten.

user1.domain.com/?site=user2 will display user2.domain.com. (intern it is www.domain.com/index.php?site=user2)

So, I tried to make the site redirect from, ?site=user2 to user2.domain, but everytime I use the [R]-modifier the url is changed so you can see my local folders.
RewriteBase /path/to/basefolder didn't help there either.

Does anybody of you know how to change the addressbar, so that it is actually not /?site=user1 or /index.php?site=user1, but user1.domain.com ?
Or maybe what I did wrong?^^

Here is my current Code:

RewriteEngine on
Options +FollowSymlinks

RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /([^\?]+)\?(.*?)&?site=([^&\ ]+)([^\ ]*)
RewriteRule ^ http://%4.domain.com/%2?%3%5 [L,R]

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_URI} !index\.php [NC]
RewriteCond %{REQUEST_URI} !\.css$ [NC]
RewriteCond %{REQUEST_URI} !\.png$ [NC] 
RewriteRule \.php index.php

RewriteCond %{HTTP_HOST} ^(.*).domain.com$
RewriteCond %{REQUEST_URI} !^/.*/
RewriteRule (.*) index.php?site=%1 [NC,QSA,L]

<Files .htaccess>
order allow,deny
deny from all
</Files>
Was it helpful?

Solution

Try adding this above your current rules (just below Options +FollowSymlinks):

RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /([^\?]*)\?(.*?)&?site=([^&\ ]+)([^\ ]*)
RewriteRule ^ http://%4.domain.com/%2?%3%5 [L,R]

So when someone types into the URL address bar something like: http://domain.com/some/path/to/file.php?foo=bar&site=user2&other=param, they'll get redirected to (and the address bar changed to): http://user2.domain.com/some/path/to/file.php?foo=bar&other=param

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top