-1

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

I want to make the directory www.domain.com/folder1/index.php?name=rock to rock.domain.com.

As for another one, I want to make www.domain.com/folder1/folder2/index.php?id=5 to 5.domain.com

Are there any way to do this? I'm a beginner in mod-rewrite. Really appreciate your help. Thanks

Additional Information

I need both of them. They will have different variables.

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

Thank you

Redzwan Latif
  • 886
  • 3
  • 14
  • 38

2 Answers2

0

In the htaccess file in your document root, you can add rules specific for "rock" and "5":

RewriteEngine On

RewriteCond %{HTTP_HOST} ^rock\.domain\.com$ [NC]
RewriteRule ^$ /folder1/index.php?name=rock [L,QSA]

RewriteCond %{HTTP_HOST} ^5\.domain\.com$ [NC]
RewriteRule ^$ /folder1/folder2/index.php?name=5 [L,QSA]

If you want it to redirect in the other direction then you'd need:

RewriteEngine On

RewriteCond %{QUERY_STRING} ^state=(.*)$ 
RewriteRule ^folder1/index\.php$ http://%1.domain.com/? [L,R=301]

RewriteCond %{QUERY_STRING} ^name=(.*)$ 
RewriteRule ^folder1/folder2/index\.php$ http://%1.domain.com/? [L,R=301]
Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • Thank you. But what if the rock and 5 will change based on php result. Another word, it's flexible and the word can be anything. – Redzwan Latif Oct 08 '13 at 05:56
  • @RedzwanLatif how are you supposed to tell the difference between what goes to `/folder1/index.php` and what goes to `/folder1/folder2/index.php`? – Jon Lin Oct 08 '13 at 06:04
  • It redirects back to domain.com instead of uniquename.domain.com. The other answers also do the same. – Redzwan Latif Oct 08 '13 at 09:17
0

Following generic rule should work for you:

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]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • do you want `/folder1/folder2/index.php` or `/folder1/index.php`? – anubhava Oct 08 '13 at 06:02
  • I need both of them. They will have different variables. 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. Thank you – Redzwan Latif Oct 08 '13 at 06:05
  • It redirects back to domain.com instead of uniquename.domain.com. The other answers also do the same. – Redzwan Latif Oct 08 '13 at 09:16
  • What is the original URL you entered in your browser. Is it `http://www.domain.com/folder1/index.php?state=rock` ? – anubhava Oct 08 '13 at 15:16