Remove subdomain from url

scottg

New Member
Does anyone know a good way to this?
FIrst first get the current URL (which always contains a subdomain) then remove the subdomain and display the result?

For Example, turn this:
<a href ="http://shop.mydomain.com/subdirectory">Link</a>

into this:
<a href ="http://mydomain.com/subdirectory">Link</a>

I've been experimenting but haven't found a good solution yet that keeps the subdirectory part of the domain intact
 

conor

New Member
Do you mean you want to redirect users from http://shop.mydomain.com/subdirectory to http://mydomain.com/subdirectory ? If so are you using Apache?

Or do you simply want to remove the subdomain? Here's how to do that in PHP, but I can't see how it would be a valid link. Apache mod_rewrite is probably what your looking for as I said above. Anyway if you simply want to change the appearance of the link here's a function in PHP i wrote a while back, I have alltered it to your needs:

Code:
/**
 * remove_subdomain
 *
 * Accurately calculates the URL and
 * removes the subdomain, with
 * support for https and different ports.
 * 
 * @access public
 * @return string
 */
function remove_subdomain(){
        $url='http';

        if($_SERVER['HTTPS']=='on')
                $url.='s';
        
        $name=explode('.',$_SERVER['SERVER_NAME']);
        $name=implode('.',array_slice($name,1));

        $url.='://'.$name;

        if($_SERVER['SERVER_PORT']!='80')
                $url.=':'.$_SERVER['SERVER_PORT'];

        $url.=$_SERVER['REQUEST_URI'];

        return $url;
}

The above function will return the url minus the subdomain, but it wont work properly if you execute it when its not in the subdomain. You call it like this:

Code:
$url=remove_subdomain();
echo $url;
 

scottg

New Member
Worked perfectly!

Thank you so much!
I cant do it in Apache, because I don't want every url to drop the subdomain. Just for certain links on the page that are generated by my cms, which is installed on the subdomain.


I have one more question:

How would you take that domain (minus the subdomain)
and use it in a header redirect.

<?php
header('Location: $url');
?>
 
Top