Solved

Creating sub-domains with PHP - is .htaccess the best way?

Posted on 2010-09-18
4
273 Views
Last Modified: 2012-05-10
I've got a need to create sub-domains automatically in code as part of a project that i'm working on.  Having surfed around the web its seems one of those things that different people have got different views on.

Can anybody suggest the best option?  My thought was to set up a section in the .htaccess file, so that anything.domainname.tld gets redirected to domainname.tld/anything.  Would that work? Is it secure and is it the most efficent way of doing things?  

Programmically we are using php and a bit of python, if anyone can suggest other ways? Are there any code frameworks for php that provide an easier or better way of working where sub-domins are concerned?
0
Comment
Question by:lynxIT
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
trencH87 earned 125 total points
ID: 33707418
I would have used .htaccess as it is reliable. Im not sure if it is even possible with PHP, unless you edit your apache configuration with the php-script. Thats not safe, so stick with .htaccess.

I'll give you an example using htaccess. Not testet, so let me know if it throws any warnings.
# Turn engine on
RewriteEngine On

# Disable Multiviews as you don't need it.
Options -MultiViews

# The main conditions
RewriteCond %{REQUEST_URI}!^/
RewriteCond %{HTTP_HOST}!^www\.domain\.com [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com
RewriteRule (.*) /%1/$1 [L]

Open in new window

0
 
LVL 13

Expert Comment

by:p_nuts
ID: 33707518
what server type.. most Apache servers automatically treat a subfolder under the root as a subdomain.
0
 
LVL 1

Author Comment

by:lynxIT
ID: 33707607
@trencH87,
Thanks for that.  Quick question (I know the basics of how RewriteCond works) but what are Multiviews?

There are a couple of sub-domains that we have set up in the apache configuration, i.e. admin.domainname.com etc... how would this htaccess rewrite affect those?

I'll try it out and come back to you!

@P-nuts:
We're running CentosOS
0
 
LVL 4

Expert Comment

by:trencH87
ID: 33707663
You can read more about the issues with MultiViews here: http://www.gerd-riesselmann.net/archives/2005/04/beware-of-apaches-multiviews

If the rewrite failes your already added subdomains, just exclude them in .htaccess.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now