?
Solved

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

Posted on 2010-09-18
4
Medium Priority
?
283 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 500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

764 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