Advertisement:

Author Topic: Subdomains for each location  (Read 14592 times)

webbuster

  • Newbie
  • *
  • Posts: 32
Subdomains for each location
« on: January 03, 2012, 11:13:32 am »
Hey guys I found this and thought this might be useful to anyone who is willing to help create a plugin that allows a subdomain for each location.



Mod Rewrite Subdomains                                                      


You might have noticed some sites having their URLs written as if they were different subdomains of the same domain. For example, for a news site that has the dynamic links of the following form:
http://yournewsdomain.com?topic=it    -  link that goes to IT news page

http://yournewsdomain.com?topic=business    - link that goes to business news page

, to be rewritten in this form:
http://it.yournewsdomain.com

http://business.yournewsdomain.com

You can achieve this without actually creating the subdomains on your server. This can be done with Apache mod_rewrite.

For this method to work you need a server that runs Apache and has mod_rewrite module installed. Also, if you don't have access to server configuration files, then you might need some help from your hosting support for the second step and possibly with the fists step of this tutorial.

 

Step 1. Create a wildcard DNS A record in your DNS Zone.

A wildcard DNS record is a record reprezented by the character "*" , that will match all non existing subdomain names. For example *.yournewsdomain.com will match for  key1.yournewsdomain.com , key2.yournewsdomain.com and so on.

You will need to edit the DNS zone records for your domain and add an A record for *.yournewsdomain.com. , pointing to the IP for your domain.

If your domain  is on a shared hosting account, then you will need to search in your control panel for a section for DNS management. Enter a new A type DNS record, set as name "*" and enter the IP for your domain. If you cannot find a DNS management section on your hosting account, then you will have to ask your hosting support to add it for you.

 

Step 2.  Setup wildcard ServerAlias

You must now configure Apache server to serve the pages that come for any subdomain of your domain. Do this by adding a wildcard ServerAlias.

Add the following line to httpd.conf VirtualHosts section for your domain:
ServerAlias www.yournewsdomain.com yournewsdomain.com *.yournewsdomain.com

Again, if you are on a shared hosting and don't have access to httpd.conf file, and no means in your control panel to edit your VirtualHosts section, then you must  ask help from your hosting support.

 

Step 3. Add rewrite rules to your .htaccess file

First you must prepare your .htaccess file . If the file does not exist, create it.

You might already have these lines in your .htaccess files, if you do don't add them again.
Options +FollowSymLinks

You need this option enabled for your rewrite rules to work. But in most cases is already enabled in the configuration files for the http server. 
RewriteEngine On

Use this line if RewriteEngine is not already enabled on your server.
RewriteBase /

If your code is in the root of your domain, then use / as the rewrite base, like in the line above. If it is in a folder, then use the path to that folder, for example:
RewriteBase /subdomain/

Add the actual rewrite rules:
RewriteCond %{HTTP_HOST} !www.yournewsdomain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).yournewsdomain.com [NC]
RewriteRule (.*) index.php?topic=%2 [NC,QSA]

First 2 lines add the condition for the actual rewrite rule (line 3) to come in place.

First line will add an exception for the subdomain www. Add here a similar line for each subdomain that you don't want to follow the same rule.

Second line adds the condition that the URL is in a form that looks like a subdomain , with or without the www string in front (http://anything.yournewsdomain.com).

The actual rule is the third line, and will translate any URL that matches the above conditions to a form: http://yournewsdomain.com?topic=%2. 

Some things explained in the rewrite rules:

1. %2 is a back-reference that is created in the second condition. Back-references are created by groups of parentheses in the condition pattern. So in this case, the first parenthesis (www.) will create first back-reference that can be used with %1, and the second one ([a-z0-9-]) , that matches the subdomain like form, will create the second back-reference, which we use with %2.

2. The flag NC at the end of the conditions and rule means that the comparisons should be made case insensitive (No Case).

3. The flag QSA at the end of the rule means that if there are additional queries at the end of URL string, they should be added to the URL when rewritten (Query String Append).

You might need  to make adjustments to the rewrite rule form, for example instead of index.php you have other landing page, or instead of the parameter "topic" you have other.

Tulegen.S

  • Newbie
  • *
  • Posts: 19
Re: Subdomains for each location
« Reply #1 on: April 24, 2012, 08:33:12 pm »
This is very and extreme also superb post. Thank you for your posting.

Cheers,
Tuugii

djjimmie

  • Newbie
  • *
  • Posts: 8
Re: Subdomains for each location
« Reply #2 on: June 20, 2012, 03:02:55 pm »
Awesome post! Going to try this. Thank you

Legion

  • Hero Member
  • *****
  • Posts: 622
  • I am founder of top20remedies.com buyeradvise.com
Re: Subdomains for each location
« Reply #3 on: November 03, 2012, 10:57:18 pm »
lets see how it works ;)

diveshlalit

  • Guest
Re: Subdomains for each location
« Reply #4 on: November 05, 2012, 11:48:39 am »
nice but very difficult can any one help me

Legion

  • Hero Member
  • *****
  • Posts: 622
  • I am founder of top20remedies.com buyeradvise.com
Re: Subdomains for each location
« Reply #5 on: November 07, 2012, 02:13:16 pm »

how it is going to be work? will it wok on a single database?  if user register at www.jobs.mysite.com and then if he tries to login at mysite.com will it work?

diveshlalit

  • Guest
Re: Subdomains for each location
« Reply #6 on: November 07, 2012, 03:10:25 pm »
yes its work www.onlyads.net it is base on dns server but many webhosting not providethis services

BuySellAndSwap

  • Sr. Member
  • ****
  • Posts: 389
Re: Subdomains for each location
« Reply #7 on: November 10, 2012, 11:07:18 pm »
can anyone help me how to do this please?

Thank you

diveshlalit

  • Guest
Re: Subdomains for each location
« Reply #8 on: November 11, 2012, 04:24:38 pm »
can anyone help me how to do this please?

Thank you

you have a wildcard dns server

BuySellAndSwap

  • Sr. Member
  • ****
  • Posts: 389
Re: Subdomains for each location
« Reply #9 on: November 11, 2012, 04:37:24 pm »
I do yes but I just don't know how to set up the rewrite rule is step 3 thanks for your help

Legion

  • Hero Member
  • *****
  • Posts: 622
  • I am founder of top20remedies.com buyeradvise.com
Re: Subdomains for each location
« Reply #10 on: November 11, 2012, 07:20:27 pm »
i followed all steps now wht to do

BuySellAndSwap

  • Sr. Member
  • ****
  • Posts: 389
Re: Subdomains for each location
« Reply #11 on: November 11, 2012, 08:04:54 pm »
Hi legion

Could you give me a copy of you htaccess file so I can see how to do it if possible please?

Thank you

Legion

  • Hero Member
  • *****
  • Posts: 622
  • I am founder of top20remedies.com buyeradvise.com
Re: Subdomains for each location
« Reply #12 on: November 12, 2012, 04:54:54 pm »
hi,
htacess had some internal server  error so i removed it

diveshlalit

  • Guest
Re: Subdomains for each location
« Reply #13 on: November 14, 2012, 04:02:02 pm »
i think osclasss not support this service
thanks

umerkakar

  • Newbie
  • *
  • Posts: 6
Re: Subdomains for each location
« Reply #14 on: January 09, 2013, 08:51:48 pm »
I done all the steps. But only my front webpage is opening with any sub domain name. but every link stop working. please help me.