0

I want like this Ex:

http://user1.mysite.com or http://user2.mysite.com if anyone enter URL Like these on browsers it should go as bellow

http://mysite.com/user.php?userName=user1

How can i solve this with .htaccess

With help of Prix i solved this Issue

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.mysite\.com [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.mysite\.com
RewriteRule ^(.*)$ http://mysite.com/user.php?userName=%1 [R=301]

Its working fine :)

http://user1.mysite.com
http://mysite.com/user.php?userName=user1 -> This one is working fine with Prix Code

Now i want Like Bellow. I have tried lot but not working. So again i don't have anyway rather than ask here.

I want like this

http://user1.mysite.com/inbox/
http://mysite.com/inbox.php?userName=user1

FIXED

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.mysite\.com [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.mysite\.com
RewriteRule ^inbox/$ http://mysite.com/inbox.php?userName=%1

And also Like this

http://user1.mysite.com/message/1
http://mysite.com/view-message.php?userName=user1&messageID=1

FIXED

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.mysite\.com [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.mysite\.com
RewriteRule ^message/(.*)$ http://mysite.com/view-message.php?userName=%1&messageID=$1 [R=301]
rkaartikeyan
  • 1,977
  • 9
  • 29
  • 57
  • 1
    I am rather confused, so you want it to look like `http://user1.mysite.com` and the masked url to be `http://mysite.com/user.php?userName=user1`? – Prix Jun 30 '13 at 13:04
  • yes exactly bro As i asked if you go user1.ddiapps.com then you can see its redirect in url as ddiapps.com/user.php?userSubDomain=user1 :( – rkaartikeyan Jun 30 '13 at 13:06
  • I have updated my answer try the 2nd rule – Prix Jun 30 '13 at 13:07
  • @Prix Not working bro... – rkaartikeyan Jun 30 '13 at 13:15
  • 1
    I am not sure that you can actually do that with the subdomain, it would be easier if you simple create your subdomain pointing to the folder subdomains and there have a index.php that gets the username from the url. – Prix Jun 30 '13 at 13:20
  • You cannot mask different domains because it will make the htaccess redirect it. You can however do `http://user1.mysite.com/inbox/` mask `http://user1.mysite.com/inbox.php?userName=user1`. It would be better if you make a new question as your initial question and needs have changed a lot and keeping 1 single question with updates is not viable here on SO as it disrupts the meaning of the approved answers and other answers. – Prix Jul 01 '13 at 03:36

1 Answers1

1

Using condition like this should do it:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.mysite\.com [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.mysite\.com
RewriteRule ^(.*)$ http://mysite.com/user.php?userName=%1 [R=301]

Keep in mind it will redirect all the sub-domains.

Prix
  • 19,417
  • 15
  • 73
  • 132
  • @rkaartikeyan oh my bad you want to do the opposite of what I mentioned above let me rewrite. – Prix Jun 30 '13 at 11:30
  • Sure bro... i am waiting :) – rkaartikeyan Jun 30 '13 at 11:32
  • @rkaartikeyan done, this one should do what you asked, keep in mind that you need to have control over the sub-domains or it wont work. – Prix Jun 30 '13 at 11:33
  • Bro... i hope there is something wrong with my hosting... because its not working for me :( – rkaartikeyan Jun 30 '13 at 11:36
  • @rkaartikeyan it's possible that your subdomain is pointing to a different IP or not hosting within your main domain which would make this not work. – Prix Jun 30 '13 at 11:38
  • @rkaartikeyan do you have cPanel ? if you do make sure the subdomain is set to be an alias of your root domain. – Prix Jun 30 '13 at 11:39
  • Bro yes i have cPanel... how can i make sure the subdomain is set to be an alias of my root domain? – rkaartikeyan Jun 30 '13 at 11:41
  • bro i have this on DNS Record *.ddiapps.com. A 159.253.138.98 – rkaartikeyan Jun 30 '13 at 11:45
  • @rkaartikeyan but you need the subdomain configured on your webserver as well otherwise it won't work. Unless your host can wildcard your domain on the webserver you will still have to create the subdomains, you can automate it with php and cpanel API. – Prix Jun 30 '13 at 11:45
  • oh... so without creating subdomain... this will not work right? – rkaartikeyan Jun 30 '13 at 11:49
  • now i created user1 as subdomain on my cpanel. still its not working. anyway will contact the host provider. and really i am so thankful to you for spent your time to me :) – rkaartikeyan Jun 30 '13 at 11:50
  • 1
    @rkaartikeyan Yes the DNS wildcard is just to point the domain to an IP, if that IP does not have a webserver or if the webserver is not configured to receive that it will show the default page that you are seeing. So there are 3 options in your case: 1) create the subdomains manually or 2) ask your hosting company if they can make a wildcard record on your virtualhost for that or 3) use the cPanel API to automatically create subdomains for your users when they signup. – Prix Jun 30 '13 at 11:51
  • @rkaartikeyan there is a small mistake on your rule change `userName=$1` to `userName=%1` – Prix Jun 30 '13 at 11:52
  • Bro did your code will be redirected on URL? or is it just masked? – rkaartikeyan Jun 30 '13 at 12:12
  • 1
    @rkaartikeyan you can try it here http://htaccess.madewithlove.be/ and see the result. – Prix Jun 30 '13 at 12:15
  • Bro my host provided fix something... now its woking please try http://user1.ddiapps.com then its goes http://ddiapps.com/user.php?userSubdomain=user1. Really thanks for your great help but i want this as masked :) like we do http://mysite.com/service/service1 to http://mysite.com/view-service.php?service1. Like i need masked can you help me? – rkaartikeyan Jun 30 '13 at 12:21
  • 1
    @rkaartikeyan So can you update an example on your question? On how you would like it to work. – Prix Jun 30 '13 at 12:26
  • Now i have a problem :( if i enter www.mysite.com its redirecting :( bro please help me to solve the issue – rkaartikeyan Jun 30 '13 at 13:57
  • @rkaartikeyan add this `RewriteCond %{HTTP_HOST} !^www\.mysite\.com [NC]` see update – Prix Jun 30 '13 at 13:59
  • RewriteEngine on RewriteCond %{HTTP_HOST} !^www\.ddiapps\.com [NC] RewriteCond %{HTTP_HOST} ^(.*)\.ddiapps\.com RewriteRule ^(.*)$ user.php?userName=%1 [L] Not working bro. – rkaartikeyan Jun 30 '13 at 14:04
  • @rkaartikeyan what is not working the www still going to it or the mask ? – Prix Jun 30 '13 at 14:07
  • @rkaartikeyan thats because it won't work as you can't make it a index, you will have to use the idea I mentioned on your question commend "create your subdomain pointing to some folder of your preference and there have a index.php that gets the username from the URL" – Prix Jun 30 '13 at 14:29
  • @rkaartikeyan after creating the subdomain to a specific folder, follow this answer starting from step 3 the PHP part: http://stackoverflow.com/a/586160/342740 – Prix Jun 30 '13 at 15:01
  • Bro i have solved this issue... Thanks a lot for your kind time. if you don't mind add me on your google + rkaartikeyan@gmail.com – rkaartikeyan Jun 30 '13 at 15:35