Jump to content

Possible to strip incoming URL of sub domain and execute script?


msaz87

Recommended Posts

Hey all,

 

So the general idea with what I'm looking to do is find a method to take an inbound URL that includes a subdomain, strip out just the sub, then execute something off of it. The idea is to have each client on the site able to say to their customers "go to example.site.com" -- without actually creating a subdomain for each.

 

They would input that URL and (here's where I'm unsure) either the 404 or whatever strips out "example" then I have a script that checks the DB for whether there is a client by that name and if so, redirects them to site.com/?client=example or whatever.

 

Hopefully that makes sense... any advice is greatly appreciated. Thanks!

You will probably need to use .htaccess to be able to do what you are asking for.

 

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.yourwebsite.com
RewriteCond %{HTTP_HOST} ([^.]+)\.yourwebsite.com
RewriteRule ^(.*)$ /path_to_your_site/httpdocs/work_out.php?url=%1

 

This *should* grab any subdomain (e.g. mysubdomain.yoruwebsite.com) and redirect to a page called work_out.php (e.g. www.yourwebsite.com/work_out.php?url= ***subdomain***).  This way you can write a PHP script on the work_out.php page which redirects the user to www.yourwebsite.com/subdomain... for example.

 

BUT...

 

As your question poses, I suspect you are more interested in having subdomains that STAY that way... for example a user would navigate to: mysubdomain.yourwebsite.com.  While at that site links would be: mysubdomain.yourwebsite.com/downloads, etc. so they consistently stay with the subdomain instead of to a folder based on the main domain... (I hope this makes sense cuz I'm confusing myself haha).

 

In which case...

 

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ([a-z0-9-]+)/? http://$1.domain.com [R=301,NC,L]

 

This I believe should do roughly what you want, but of course you need to replace "domain.com" with your domain in both occurances.

 

Disclaimer:  I have had similar interests with this kind of thing, though I have not done it yet... so when I found this post I did a bit of research into this and these are the .htaccess methods that I found that "claim" to do what you want.  I have not tested them and I do not fully understand the code for .htaccess therefore I am only going by what I have read to be true and cannot really confirm what this does by reading it.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.