Creating subdomain to redirect any page or directory

October 28, 2014 3.2k views

Using Ubuntu 12.04 running wordpress.

I want to add subdomain to redirect any sub-directory or page.

For example:
My site: ibrahimmumcu.com
Redirect: kisa.ibrahimmumcu.com to ibrahimmumcu.com/any/subdirectory

How can I do that?

3 Answers

simple way to do this is a 2-part solution:

in the .htaccess:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.mysite.com
RewriteCond %{HTTP_HOST} ^(.+).ibrahimmumcu.com
RewriteRule ^([^/]*)$ http://www.ibrahimmumcu.com/subdirectory/%1 [P,L,QSA]

in the vhost config, add (below ServerName):

ServerAlias *.ibrahimmumcu.com

You will need to enable .htaccess by changing

AllowOverride None

To

AllowOverride All

This will change allow people to visit

 kisa.ibrahimmumcu.com

and actually get

www.ibrahimmumcu.com/subdirectory/kisa
  • Why does it take so long? Do you really think this is the easiest way to do that?

  • Actually, no. That's not the best way. It is the easiest, just not best.

    the best way is to have your code detect the sub-domain being called and serve out content for that page.

    example:

    <?php
    $URL = $_SERVER['SERVER_NAME'];
    $DOMAIN = str_replace(".ibrahimmumcu.com","",$URL);
    echo $DOMAIN
    ?>
    
  • I don't understand these PHP codes. What will the code do?

  • It's example code. What it does is grabs the URL they requested (the domain name), strips off your top level domain (ibrahimmumcu.com), then echos out what is left.

I found my best solution.

Added these lines to .htaccess file.

RewriteCond %{HTTP_HOST} ^sub\.mysite\.com$ [NC]
RewriteRule ^(.*) http://www.external-site-or-directory.com/$1 [L,R]
Have another answer? Share your knowledge.