Solved

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

Posted on 2010-09-18
4
279 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 count occurrences of each item in an array.

728 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