Jump to content

Subdomain Routing with .htaccess


charlesg

Recommended Posts

I had my server company setup wild card subdomains, so anything.mysite.com will work and display the same content as www.mysite.com.

 

Now I need some .htaccess magic setup to route any sub domain to the www.mysite.com (and all URI segments), but keep the subdomain in the location bar.

 

Unfortunately, I'm not a regex guru.  Does anyone know how to do this who is willing to help me out please?

 

Examples - I need all of these formats to work like this...

 

hello.mysite.com -> www.mysite.com, but display hello.mysite.com

hello.mysite.com/cool/site -> www.mysite.com/hello/site, but display hello.mysite.com/cool/site

hello.again.mysite.com -> www.mysite.com, but display hello.again.mysite.com

hello.again.mysite.com/cool/site -> www.mysite.com/cool/site, but display hello.again.mysite.com/cool/site

 

Link to comment
Share on other sites

I found this on a forum thread, but it didn't seem to work either.  Anyone else got an idea?

 

RewriteCond %{ENV:REDIRECT_SUBDOMAIN} ="" 
RewriteCond %{HTTP_HOST} ^([a-z0-9][-a-z0-9]+)\.domain\.com\.?(:80)?$ [NC] 
RewriteCond %{DOCUMENT_ROOT}/%1 -d 
RewriteRule ^(.*) %1/$1 [E=SUBDOMAIN:%1,L] 
RewriteRule ^ - [E=SUBDOMAIN:%{ENV:REDIRECT_SUBDOMAIN},L]

Link to comment
Share on other sites

It's a bandaid fix but I use it to separate layout from content in my website. My .htaccess routes all http errors to a particular script. This script displays either the requested webpage or a custom error message. It works because the URI_REQUEST... HTTP_REQUEST... can't remember the name of it atm but that value is still in the $_SERVER array after .htaccess reroutes the request to your script.

 

For example...

  • someone types/clicks a link with http://www.mydomain.com/somelink.php
  • 404 error is generated and redirected
  • script looks at $_SERVER variable and parses request
  • if file exists it is included, if not 404 error page displayed

 

This URL in the browser will be whatever the user typed or the HREF value.

 

 

Link to comment
Share on other sites

Here's the beginning of the code from my "master" page:

 

$end = strrpos($_SERVER['REQUEST_URI'],'.');
$page = $_SERVER['DOCUMENT_ROOT'].substr($_SERVER['REQUEST_URI'],0,$end).'.iphp';
if(file_exists($page)) {
	header("HTTP/1.x 200 OK\r\n");
	$require = true;
}

 

If you don't do the header you'll send out the 404 error as a response. And when using header() remember NO OUTPUT whatsoever before it's call.....not even a newline. You just have to modify it to parse it to your needs.... mine are pretty simple as you can see.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.