[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 850
  • Last Modified:

ht access mod rewrite subdomain to variable

Hi,
I am looking for some help with a new site I am developing. What I need to do is have members to the site being able to have their own subdomain for each account: for example

account1.XXX.com
account2.XXX.com
account3.XXX.com

What I have been looking at is being able to use PHP to write the htaccess file and rewrite the URL to a URL with a variable with out actually creating the subdomains. I supose like creating a fake subdomain for each account holder.

Therefore I think the way to do it is with .htaccess to create something like if the user enters

account1.XXX.com  they get redirected to
account1.XXX.com/index.php/?a=1234 where alll the information for the account is pulled from a mysql database using the variable 1234

What I have no clue how to do and after searching the web I cannot work out how to do the subdomain rewrite of this.

Any help to get me going would be great

Thanks
0
flashjordan
Asked:
flashjordan
1 Solution
 
gr8gonzoConsultantCommented:
Hmmm - you can do this (and it's actually a nice way of doing it - I use the "fake" subdomain method myself), but you should probably understand the whole setup process first.

STEP 1:
Either way you go, you'll need to have the DNS set up for those fake subdomains. Let's say Joe is the holder of account #2. Joe's PC / network needs to be able to look up account2.XXX.com via DNS to see which server is hosting the application.

Most DNS providers make this easy to set up by creating a DNS record on XXX.com for wildcards. Usually, it's as simple as creating a new subdomain called * instead of a real name like account8. Then, you make that subdomain point to the IP address of your server.

STEP 2:
Once Joe's PC discovers the wildcard DNS and sends his browser to your server, his browser sends what host name he used (account2.XXX.com) to the web server.

(If you're using SSL, this gets a tiny bit more complicated, so if you're planning on using SSL, you may want to say so now)

The web server reads in the host name  (account2.XXX.com) and matches it up to an entry in its configuration files. Since you're using Apache, it would probably match up to a <VirtualHost> entry with the correct IP address and a ServerName or ServerAlias of *.XXX.com.

NOW the browser's request is finally handled by a file on the server, like .htaccess. By this time, though, it would be far more efficient for PHP to just look up the subdomain instead of using mod_rewrite in an .htaccess file.

While you -can- use a mod_rewrite, two things to consider if you'll have a lot of visitor traffic:

1. Using .htaccess files slows down the web server request. If you use the Apache config variable AllowOverride to allow htaccess files, Apache will have to do some extra processing every time the browser tries to get a file from that site. So if the browser gets account2.XXX.com/images/logo.gif, it will undergo the same .htaccess processing, even if there's no resulting difference.

(The act of Apache having to check for an .htaccess file and process it if its there is slower than just delivering content right away.)

2. Mod_rewrite is pretty cool, but it's also slow.

If you don't think you'll have a lot of traffic (e.g. hundreds of visitors every second), then you probably won't notice a difference, but sometimes it's good to learn and start using more efficient ways of doing things so you don't even have to think about them for later projects.

Here's some code for whichever path you choose:

METHOD #1: PHP to extract the subdomain (better)
<?
$pieces = split("\.",$_SERVER["HTTP_HOST"];
$subdomain = $pieces[0];
?>

METHOD #2: Using .htaccess (untested, but it should work)
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^account([0-9]+)
RewriteRule ^(.*)$ http://www.XXX.com/index.php?accountID=%1 [R=301,L]
0
 
flashjordanAuthor Commented:
This was perfect, great thanks for putting me in the right direction
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now