1

I have a wildcard subdomain *.domain.com assigned to public_html/.

I want to do like this:

For example, /folder1/index.php is based on state name(?state=statename).

For the /folder1/folder2/index.php, it will be based on unique name(?name=uniquename).

So, www.domain.com/folder1/index.php?state=statename will be statename.domain.com

and www.domain.com/folder1/folder2/index.php?name=uniquename will be uniquename.domain.com

This is my code

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+folder1/index\.php\?state=([^\s&]+) [NC]
RewriteRule ^ http://%1.domain.com/? [R=301,L]

RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+folder1/folder2/index\.php\?name=([^\s&]+) [NC]
RewriteRule ^ http://%1.domain.com/? [R=301,L]

The problem is it redirect back to public_html directory. Is there any problem with the code?

Old Code

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^$ /index [L]

RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([^.]+).domain.com$ [NC]
RewriteRule ^(index)/?$ /$1.php?name=%2 [L,NC,QSA]

Code explanation : Whenever user enter uniquename.domain.com, it will automatically go to www.domain.com/index.php?name=uniquename and the uniquename.domain.com in url bar would not change.

The differences for the new problem is there are different state directory and the domain would be state1.uniquename.domain.com. The 'state1.uniquename.domain.com' in the url bar should not change too.

Redzwan Latif
  • 886
  • 3
  • 14
  • 38
  • I remember writing those rules :P please explain what didn't work? I don't get what you mean by `it redirect back to public_html directory` – anubhava Oct 08 '13 at 14:29
  • Whenever I type uniquename.domain.com, it goes to domain.com(public_html directory) instead of uniquename.domain.com(www.domain.com/folder1/folder2/index.php?name=uniquename).Thanks – Redzwan Latif Oct 08 '13 at 14:44
  • But you are typing `www.domain.com/folder1/folder2/index.php?name=uniquename` in browser right? – anubhava Oct 08 '13 at 14:55
  • Duplicate of http://stackoverflow.com/questions/19240015/wildcard-subdomain-mod-rewrite – CBroe Oct 08 '13 at 14:55
  • Do you have any PHP/CMS framework installed in any of these domains? – anubhava Oct 08 '13 at 14:59
  • @anubhava What if I want to type uniquename.domain.com in the url bar and it will automatically detect it as www.domain.com/folder1/folder2/index.php?name=uniquename. Thank you :D – Redzwan Latif Oct 08 '13 at 15:21
  • Nope I didn't use any framework or CMS. – Redzwan Latif Oct 08 '13 at 15:21
  • Ok but how do you distinguish between `uniquename.domain.com` vs `state.domain.com`? – anubhava Oct 08 '13 at 15:23
  • It's in different directory. In the uniquename directory, I will detect the uniquename. In the state directory, I'll detect the state. I'll use php url $_GET parameter – Redzwan Latif Oct 08 '13 at 15:39
  • I am still not getting it. Please try to explain what should be behavior when user enters: `uniquename.domain.com` vs `state.domain.com` ? – anubhava Oct 08 '13 at 16:04
  • when user enter uniquename.domain.com, it should go to www.domain.com/folder1/folder2/index.php?name=uniquename. When user enter state.domain.com it should go to www.domain.com/folder1/index.php?state=statename . The only different between these two is the directory – Redzwan Latif Oct 08 '13 at 16:22
  • But rewrite rule can't differentiate by just looking at: `uniquename.domain.com vs state.domain.com` since there is no info in the URL that tells it to redirect to `/folder1/folder2` or `/folder1` – anubhava Oct 08 '13 at 16:23
  • I think I get it now. What if there are different folder/directory for each state. Let's say that there's two state. state1 is on public_html/state/state1/ directory and another one is public_html/state/state2/. And the url should be state1.uniquename.domain.com or state2.uniquename.domain.com. Is it possible to do this or do you have any other ways than this? Thank you – Redzwan Latif Oct 08 '13 at 16:32
  • If you have separate directories like `/state/state1`, `/state/state2` then it might be possible. – anubhava Oct 08 '13 at 16:50
  • Good to hear that. How the .htaccess code should be? Thanks :D – Redzwan Latif Oct 08 '13 at 16:54
  • check the answer below. – anubhava Oct 08 '13 at 17:18

1 Answers1

1

Based on your comments. Make sure DOCUMENT_ROOT for www.domain.com, state1.domain.com, state2.domain.com is public_html

Try this code:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+folder1/index\.php\?state=([^\s&]+) [NC]
RewriteRule ^ http://%1.domain.com/? [R=301,L]

RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+folder1/folder2/index\.php\?name=([^\s&]+) [NC]
RewriteRule ^ http://%1.domain.com/? [R=301,L]

RewriteCond %{REQUEST_URI} !\.(?:jpe?g|gif|bmp|png|tiff|css|js)$ [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.([^.]+)\.domain\.com$ [NC]
RewriteRule ^ /state/%1/client/index.php?name=%2&page=%{REQUEST_URI} [L,NC]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • So, if I type state1.uniquename.domain.com in the url bar, will it be identified as www.domain.com/state/state1/index.php?name=uniquename ? – Redzwan Latif Oct 08 '13 at 17:26
  • I see so if that is how you want then let me edit it. – anubhava Oct 08 '13 at 17:29
  • But how do you want `http://state1.uniquename.domain.com/something` to be handled? – anubhava Oct 08 '13 at 17:31
  • For now, there is only one page which is index.php. so it would be http://state1.uniquename.domain.com/index.php but is it possible to show the url without the 'index.php'? – Redzwan Latif Oct 08 '13 at 17:35
  • Ok see the edit. It will make `http://state1.uniquename.domain.com/something` to `www.domain.com/state/state1/index.php?name=uniquename&page=/something` – anubhava Oct 08 '13 at 17:39
  • I just need to add one more directory so it would be `http://www.%3/state/%1/client/index.php?name=%2&page=%{REQUEST_URI} [R,L,NC]` but it redirects to public_html – Redzwan Latif Oct 08 '13 at 17:54
  • Okay now it works after I delete line 2-7. But the url bar shows www.domain.com/state/state1/index.php?name=uniquename&page=/something instead of just http://state1.uniquename.domain.com/something – Redzwan Latif Oct 08 '13 at 17:58
  • If you're changing domains of URL then URL also changes. Let me ask here is DOCUMENT_ROOT same for `www.domain.com` and `state1.uniquename.domain.com` ? – anubhava Oct 08 '13 at 18:00
  • Yes. Both is assigned to public_html. I think you've helped me before this. Let me edit the question for the old code. – Redzwan Latif Oct 08 '13 at 18:05
  • From the old code comment last 3 lines and use all suggested code here. – anubhava Oct 08 '13 at 18:15
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/38817/discussion-between-redzwan-latif-and-anubhava) – Redzwan Latif Oct 08 '13 at 18:20