Solved

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

Posted on 2010-09-18
4
275 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

778 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